note 43898 added to function.date
| From: | joe at rn2 dot php dot net | Date: | Wed, 07 Jul 2004 22:38:30 +0000 |
| Subject: | note 43898 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72839@lists.php.net to get a copy of this message | ||
From all my reading in this section of the site, it amazes me how complex everyone seems to be
making their date calculation functions! For most date operations the time (cpu cycles? definately
programmer time anyway...) it takes to parse a date string into a unix timestamp using the
strtotime() function is minimal compared to the time it takes to do it any other way:
<?php
// Returns number of seconds between low_date and high_date
function date_diff_seconds ( $low_date, $high_date ) {
return strtotime($high_date)-strtotime($low_date);
}
// Returns number of minutes between low_date and high_date
function date_diff_minutes ( $low_date, $high_date ) {
return floor(date_diff_seconds($low_date,$high_date)/60); // 60 seconds per minute
}
// Returns number of hours between low_date and high_date
function date_diff_hours ( $low_date, $high_date ) {
return floor(date_diff_seconds($low_date,$high_date)/3600); // 3600 seconds per hour
}
// Returns number of hours between low_date and high_date
function date_diff_days ( $low_date, $high_date ) {
return floor(date_diff_seconds($low_date,$high_date)/86400); // 86400 seconds per day
}
$now=date("Y-m-d H:i:s");
$then=date("Y-m-d H:i:s",time()-(60*60*24*365)); // 1 year ago
echo "Difference between '$now' and '$then' is:\n";
echo date_diff_seconds($then,$now) . " seconds, or ";
echo date_diff_minutes($then,$now) . " minutes, or ";
echo date_diff_hours($then,$now) . " hours, or ";
echo date_diff_days($then,$now) . " days.\n";
?>
Output:
Difference between '2004-07-07 15:40:56' and '2003-07-08 15:40:56' is:
31536000 seconds, or 525600 minutes, or 8760 hours, or 365 days.
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43898
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43898&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43898&report=yes
Search -- http://master.php.net/manage/user-notes.php