Generating a timestamp with the mktime() function
May 28, 2002
It's all very well formatting a timestamp if you already have one, or can
generate it from the current time, but you're going to need to create your own
timestamps as well, based upon datetimes in other formats, such as from a
database. The mktime() function is the one to use here - given a date and time,
it generates a timestamp. It takes up to seven parameters, all integers, as
follows: mktime(hour,minute,second,month,day,year,is_dst) Note that the
order of the parameters is a little odd, and the cause of much anguish! Any of
the parameters can be left out, from right to left, and the current time will be
used to calculate the rest. For example, if you only enter the first five
parameters, the timestamp generated assumes the current year. The final
parameter stands for daylight saving time (1 for yes, 0 for no, and -1 for lt
PHP work it out). Some examples:
// timestamp for 23:45:37 on 28 Nov 2002
print mktime(23,45,37,11,28,2002);
displays:
1038519937
mktime() makes date and time calculations extremely easy by automatically
correcting the date and time integers that are outside their boundaries. For
example, if we wanted a timestamp of exactly four days beyond the one we
generated in the previous example, we just need to add four to the days integer.
Even though this produces 32, PHP will automatically work it out as if it was
the 2nd of December. We'll check that they are the same by comparing the
'incorrectly' formatted one to the 'correctly' formatted one that follows.
// timestamp for 23:45:37 on "32 Nov" 2002
print mktime(23,45,37,11,32,2002);
print "<BR>";
// timestamp for 23:45:37 on 2 Dec 2002
print mktime(23,45,37,12,2,2002);
displays the same timestamps for both:
1038865537
1038865537
In order for this function to be really useful, we have to imagine that we
get the datetime from elsewhere, perhaps a database. Although string
manipulation is beyond the scope of this article, we'll look at a common
example, where the date and time are read from a MySQL database. Then to
demonstrate the ease of date calculation, we'll display the datetime 2 months, 3
days and 21 minutes ahead in time
// assume we get this from a MySQL database
$mysql_date = '2002-08-15 13:21';
$mysql_year = substr($mysql_date,0,4);
$mysql_month = substr($mysql_date,5,2);
$mysql_day = substr($mysql_date,8,2);
$mysql_hour = substr($mysql_date,11,2);
$mysql_minute = substr($mysql_date,14.2);
print "old date: $mysql_year-$mysql_month-$mysql_day
$mysql_hour:$mysql_minute";
print "<BR>";
$new_timestamp =
mktime($mysql_hour,
$mysql_minutes+21,0,
$mysql_month+2,
$mysql_day+3,
$mysql_year);
print "new date: ".date("Y-m-d H:i",$new_timestamp);
Note:Color coded lines have been split for display purposes
This displays:
old date: 2002-08-15 13:21
new date: 2002-10-18 13:21
Calculating dates further behind is just as easy - you subtract the time
period/s from the relevant variable/s
There is also a related gmmktime() function, which is identical to mktime()
except that the passed parameters represent a Greenwich Mean Time date.
Testing the validity of a date with the checkdate() function
Sometimes you're not sure of the input you've received, whether from an HTML
form, or from a database itself (perhaps you're reading a VARCHAR field, not a
DATE field, so there is no guarantee that the format is valid). The checkdate()
function can come in useful here. It validates gregorian dates, and takes the
month, day and year as ingeger parameters, as follows. Again note the order the
parameters are passed. checkdate(month,day,year) The function returns a
boolean value (true if the date is valid, otherwise false). Take a look at the
following example:
if (checkdate(11,12,2002)) {
print "first date valid";
}
else {
print "first date invalid";
}
print " ";
if (checkdate(2,29,2001)) {
print "second date valid";
}
else {
print "second date invalid";
}
which displays:
first date valid
second date invalid
The function takes into account days for each month specifically, as you can
see by the fact that the second date was invalid (February only had 28 days in
2001). It also takes into account leap years. You can't rely completely on this
function for validity checking though - the year 200 is valid, although probably
your credit card system doesn't want anything to do with dates before the year
2000.
Warning - although a date might be valid, you can't use the date and time
functions on all valid dates. The general boundary is the year 1902, and the
year 2037 (this is caused by the limits of an integer, or positive and negative
2147483647. 1902 and 2037 are both 68 years away from 1970, which in seconds is
just below that limit. Going slightly into 1901 and 2038 would exceed the limit,
leading to problems. To be precise, the actual limit is Fri, 13 Dec 1901
20:45:54 GMT, and Tue, 19 Jan 2038 03:14:07 GMT. If you thought Y2K was bad,
just wait for Jan 19, 2038!
Returning the current timestamp with the time() function
Date and Time in PHP
Getting more accurate with microtime() and gettimeofday()
|