note 45668 added to function.mktime
| From: | ed | Date: | Tue, 14 Sep 2004 19:54:56 +0000 |
| Subject: | note 45668 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76685@lists.php.net to get a copy of this message | ||
calculate the difference in seconds between 2 dates. I am storing the date in this format in MySQL -
field type 'datetime' which looks like
2004-09-14 20:36:00
and I use the below to calculate the difference between current time and the time stored in the
MySQL database.
<?php
// first get the current time in this format YYYY-MM-DD HH:MM:SS
$current_time=date("Y-m-d H:i:s");
// then define the other time (I retrieved this from a MtSQL database), I've just typed it
straight in for demo purposes
$date_received='2004-09-13 21:22:49';
// then I cut up the date strings and made them into variables, could just be put into an array
though.
$year1=substr($date_received,0,4);
$month1=substr($date_received,5,2);
$day1=substr($date_received,8,2);
$hour1=substr($date_received,11,2);
$minute1=substr($date_received,14,2);
$second1=substr($date_received,17,2);
$time1=mktime($hour1,$minute1,$second1,$month1,$day1,$year1);
$year2=substr($current_time,0,4);
$month2=substr($current_time,5,2);
$day2=substr($current_time,8,2);
$hour2=substr($current_time,11,2);
$minute2=substr($current_time,14,2);
$second2=substr($current_time,17,2);
$time2=mktime($hour2,$minute2,$second2,$month2,$day2,$year2);
// then work out the difference
$time_difference=($time2-$time1);
?>
that's it!
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45668
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45668&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45668&report=yes
Search -- http://master.php.net/manage/user-notes.php