php中日期对象之间的差异


Difference between date objects in php

我需要在php中找到两个日期对象之间的差异

我试过这个:

if(strtotime($current)>strtotime($LastUpdated))
{
    $diff=strtotime($current) - strtotime($LastUpdated);
}
else
{
    $diff=strtotime($LastUpdated) - strtotime($current);
}

这给了我垃圾价值。

我也试过这个

$diff=date_diff(new DateTime($current),new DateTime($LastUpdated));

这给了我零分。

我该如何找到差异?

手册是你的朋友。-http://pt1.php.net/manual/en/datetime.diff.php带有面向对象和过程编程的示例。

从上面的链接粘贴:

OOP:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

程序:

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');

结果将是:

+2 days

玩得开心:(

<?php
$date1 = "2007-03-24";
$date2 = "2009-06-26";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days'n", $years, $months, $days);

我认为这对你更好。