Converting existing dates into Unix timestamps
Often you may have to work with data that is already in some sort of date and time
format. I opened a clients Microsoft Access database now and found that the dates
are all stored in the format YYYY/MM/DD, so inserting the current date would give
me 2000/05/27. The mktime() function can take the values of parts of a date and
turn them into a Unix timestamp.
format. I opened a clients Microsoft Access database now and found that the dates
are all stored in the format YYYY/MM/DD, so inserting the current date would give
me 2000/05/27. The mktime() function can take the values of parts of a date and
turn them into a Unix timestamp.
The format of the function is:
int mktime(int hour, int minute, int second, int month, int day, int year, int [is_dst] );
From the left you provide the hour, minute, second, month, day and year. The last argument
specifies whether you are in daylight savings time or not. It is optional and so to keep
things simple we will leave it out.
specifies whether you are in daylight savings time or not. It is optional and so to keep
things simple we will leave it out.
The script will look something like this:
<?php
echo mktime( 0,0,0,5,27,2000 );
?>
I’ve put in zeros for the hour, minute and seconds since we don’t know them and you have to
put something in there. Putting in zeros will give us midnight, which is fine. Of course
you’re probably pointing out right now that I’ve cheated and gone and put the numbers in there
myself, which is true, so let’s do it again, this time by making the script work out what to put into where:
put something in there. Putting in zeros will give us midnight, which is fine. Of course
you’re probably pointing out right now that I’ve cheated and gone and put the numbers in there
myself, which is true, so let’s do it again, this time by making the script work out what to put into where:
<?php
$access_date
= '2000/05/27';
// The explode() function splits a string by another string. In this case $access_date is
// split on the /
$date_elements = explode("/",$access_date);
// at this point
// $date_elements[0] = 2000
// $date_elements[1] = 5
// $date_elements[2] = 27
echo mktime(0,0,0,$date_elements[1],$date_elements[2],$date_elements[0]);
?>
Right, let’s get a bit more complex and imagine that instead of just getting back the date from the
Access database, we got the date and time in this format:
Access database, we got the date and time in this format:
2000/05/27 02:40:21 PM
<?php
// the string we get from Access
$date_time_string = '2000/05/27 02:40:21 PM';
// Split the string into 3 parts
- date, time and AM/PM
$dt_elements = explode(' ',$date_time_string);
// Split the date up
$date_elements = explode('/',$dt_elements[0]);
// Split the time up
$time_elements = explode(':',$dt_elements[1]);
// If we have a PM then we can add 12 hours to the hour to get into 24 hour time.
if ($dt_elements[2]=='PM')
{
$time_elements[0]+=12;
}
// output the result
echo mktime($time_elements[0],
$time_elements[1],$time_elements[2],
$date_elements[1],$date_elements[2],
$date_elements[0]);
?>