Modifying the date
Often we will want to know what the time will be in 6 hours time, what the date was 35 days ago or how
many seconds it has been since you last played Quake3. We’ve already seen how the mktime() function
can be used to generate a unix timestamp from individual date and time elements. If we wanted to
create a unix timestamp out of the current date and time, how would we go about it ? A bit of a
pointless exercise, but it will help to illustrate what we will be doing later.
many seconds it has been since you last played Quake3. We’ve already seen how the mktime() function
can be used to generate a unix timestamp from individual date and time elements. If we wanted to
create a unix timestamp out of the current date and time, how would we go about it ? A bit of a
pointless exercise, but it will help to illustrate what we will be doing later.
As we have seen, mktime() takes the following arguments: hour, minute, second, month, day, year. And
if you recall from way back in section 2, we saw that the getdate() function can get all those bits
and pieces for us.
if you recall from way back in section 2, we saw that the getdate() function can get all those bits
and pieces for us.
<?php
// get the current timestamp into an array
$timestamp = time();
echo $timestamp;
echo '<p>';
$date_time_array = getdate($timestamp);
// use mktime to recreate the unix timestamp
$timestamp = mktime(
$date_time_array['hours'],
$date_time_array['minutes'],
$date_time_array['seconds'],
$date_time_array['mon'],
$date_time_array['mday'],
$date_time_array['year']
);
echo $timestamp;
?>
Looks a little bit confusing, so what I’ll do is add in a couple of variables so that things make a little bit more sense.
<?php
// get the current timestamp into an array
$timestamp = time();
echo $timestamp;
echo '<p>';
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];
// use mktime to recreate the unix timestamp
$timestamp = mktime($hours,$minutes,$seconds,$month,$day,$year);
echo $timestamp;
?>
Now that we’ve taken the info out of the array that was created by getdate() and into
appropriately named variables, the code is a bit easier to read and understand. The great
thing now is that if we wanted to add 19 hours to the current date, instead of feeding
$hours to mktime(), we can just feed it $hours +19. mktime() will do the automatic
rollover to the next day for us!
appropriately named variables, the code is a bit easier to read and understand. The great
thing now is that if we wanted to add 19 hours to the current date, instead of feeding
$hours to mktime(), we can just feed it $hours +19. mktime() will do the automatic
rollover to the next day for us!
<?php
// get the current timestamp into an array
$timestamp = time();
echo strftime('%Hh%M %A %d %b',$timestamp);
echo '<p>';
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];
// use mktime to recreate the unix timestamp
// adding 19 hours to $hours
$timestamp = mktime($hours + 19,$minutes,$seconds,$month,$day,$year);
echo strftime('%Hh%M %A %d %b',$timestamp);
echo '<br>~E after adding 19 hours';
?>
Running this now gives me:
14h58 Saturday 03 Jun
09h58 Sunday 04 Jun
~E after adding 19 hours
09h58 Sunday 04 Jun
~E after adding 19 hours
Subtracting time is done in exactly the same way – just subtract the amount of time you want from the relevant variable.
Working out the difference between two times is also pretty straightforward. All you need to
do is have both converted into unix timestamps and you just subtract the one from the other.
The difference will be the difference between the two times in seconds. Some quick arithmetic
can convert the seconds into days, hours, minutes and seconds.
do is have both converted into unix timestamps and you just subtract the one from the other.
The difference will be the difference between the two times in seconds. Some quick arithmetic
can convert the seconds into days, hours, minutes and seconds.