note 57466 added to function.date
| From: | rubyip at gmail dot com | Date: | Wed, 05 Oct 2005 06:30:21 +0000 |
| Subject: | note 57466 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96398@lists.php.net to get a copy of this message | ||
just modified the function posted here... i forgot the name of the original author...
<?php
//date difference function
function datediff($interval, $date1, $date2) {
// Function roughly equivalent to the ASP "DateDiff" function
//convert the dates into timestamps
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$seconds = $date2 - $date1;
//if $date1 > $date2
//change substraction order
//convert the diff to +ve integer
if ($seconds < 0)
{
$tmp = $date1;
$date1 = $date2;
$date2 = $tmp;
$seconds = 0-$seconds;
}
//reconvert the timestamps into dates
if ($interval =='y' || $interval=='m') {
$date1 = date("Y-m-d h:i:s", $date1);
$date2= date("Y-m-d h:i:s", $date2);
}
switch($interval) {
case "y":
list($year1, $month1, $day1) = split('-', $date1);
list($year2, $month2, $day2) = split('-', $date2);
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) +
(date('s',$date1));
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) +
(date('s',$date2));
$diff = $year2 - $year1;
if($month1 > $month2) {
$diff -= 1;
} elseif($month1 == $month2) {
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
}
break;
case "m":
list($year1, $month1, $day1) = split('-', $date1);
list($year2, $month2, $day2) = split('-',$date2);
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) +
(date('s',$date1));
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) +
(date('s',$date2));
$diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
break;
case "w":
// Only simple seconds calculation needed from here on
$diff = floor($seconds / 604800);
break;
case "d":
$diff = floor($seconds / 86400);
break;
case "h":
$diff = floor($seconds / 3600);
break;
case "i":
$diff = floor($seconds / 60);
break;
case "s":
$diff = $seconds;
break;
}
return $diff;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57466
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57466&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57466&report=yes
Search -- http://master.php.net/manage/user-notes.php