如何计算日历月份的日期差异?

时间:2011-09-29 17:41:13

标签: php datetime date-math

$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011);
$format = '%m months';

$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$diff = $start_date->diff($end_date, true);
echo $diff->format($format);

输出“5个月”,我猜是因为DST导致一小时后关闭。但是,我需要计算日历月的差异;是否有其他类/功能可以做到这一点?


添加了一些修复:

if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$time_end += (date('I',$time_end)-date('I',$time_start))*3600; // correct for DST
$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$start_date->modify('12pm'); // ignore time difference
$end_date->modify('12pm');
$diff = $start_date->diff($end_date);
echo $diff->format($format);

这似乎给出了我想要的结果,但我还没有完全测试过它。


根据赫伯特的建议更多修正:

if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);
$diff = $start_date->diff($end_date);
$hours = $diff->format('%h');
$mins = $diff->format('%i');
$secs = $diff->format('%s');
$start_date->setTime(12,0,0);//ignore time difference for date calculations
$end_date->setTime(12,0,0);
$diff = $start_date->diff($end_date);
$years = $diff->format('%y');
$months = $diff->format('%m');
$weeks = $diff->format('%w');
$days = $diff->format('%d');

请注意,$start_date->modify('12pm')实际上并没有做任何事情。不确定为什么没有错误。

1 个答案:

答案 0 :(得分:2)

更新

在弄乱了很多不同的想法后,我发现时间戳是GMT(UTC)。 date(DATE_ATOM,$time_start)正在应用默认时区。但是,如果您设置时间戳显式DateTime将采用UTC - 因此,没有DST问题。

以下代码似乎无论时区或夏令时如何都能正常工作。

<?php

$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011)

$start_date = new DateTime();
$end_date = new DateTime();

$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);

$diff = $start_date->diff($end_date);

$format = '%m months';
echo $diff->format($format);

?>

我测试了一些边缘情况 - 日期和时间以及各种时区 - 但我没有测试每个的可能性,所以如果遇到问题,我有兴趣听到它。