Jag försöker använda mig av något liknande datediff() i ASP men nu i PHP men skriptet jag har returnerar bara 0 hela tiden. Jag undrar om någon kan hjälpa mig eller om någon har ett bättre skript. Hittade ett script på www.php.net som skall göra detta. Vet dock inte om det fungerar. Det är det första scriptet på sidan där inläggen börjar.Jämföra datum
$datum1 = "2003-12-21 10:00:00";
$datum2 = "2003-12-12 10:00:00";
echo datediff("d", $datum1, $datum2);
function datediff($interval, $date1, $date2) {
// Function roughly equivalent to the ASP "DateDiff" function
$seconds = $date2 - $date1;
switch($interval) {
case "y":
list($year1, $month1, $day1) = split('-', date('Y-m-d', $date1));
list($year2, $month2, $day2) = split('-', date('Y-m-d', $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('-', date('Y-m-d', $date1));
list($year2, $month2, $day2) = split('-', date('Y-m-d', $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;
}Sv: Jämföra datum
http://se.php.net/manual/en/function.date.php
***cut***
T.E.
07-Jan-2004 05:15
Most functions calculating the difference in days between two days take some assumptions. They often ignore leap-years or decide to set a month to 30 days. This one here should work better and more precise.
I left one thing to fix: I supposed a leap in every four years. This assumption is wrong when passing a xx00-border, if xx00 mod 400 != 0. However, PHPs date-function won't work with 1900 or less and 2100 or more. So for the next few years within the given boarders of PHP, this one should work.
function date_diff($dat1,$dat2)
/* Dat1 and Dat2 passed as "YYYY-MM-DD" */
{
$tmp_dat1 = mktime(0,0,0,
substr($dat1,5,2),substr($dat1,8,2),substr($dat1,0,4));
$tmp_dat2 = mktime(0,0,0,
substr($dat2,5,2),substr($dat2,8,2),substr($dat2,0,4));
$yeardiff = date('Y',$tmp_dat1)-date('Y',$tmp_dat2);
/* a leap year in every 4 years and the days-difference */
$diff = date('z',$tmp_dat1)-date('z',$tmp_dat2) +
floor($yeardiff /4)*1461;
/* remainder */
for ($yeardiff = $yeardiff % 4; $yeardiff>0; $yeardiff--)
{
$diff += 365 + date('L',
mktime(0,0,0,1,1,
intval(
substr(
(($tmp_dat1>$tmp_dat2) ? $dat1 : $dat2),0,4))
-$yeardiff+1));
}
return $diff;
}