Php 不是DateInterval::format函数的预期结果
可能重复:Php 不是DateInterval::format函数的预期结果,php,date,Php,Date,可能重复: 我有这样的想法: $daysDiff = intval($currentDate->diff($dueDate)->format('%R%a')); DateInterval::format的php文档中说: %R -- "+" for positive interval, "-" for negative %a -- the total count of days i the interval 如果结果有问题,请始终以天为单位返回6015,并使用正确的符号+或-
我有这样的想法:
$daysDiff = intval($currentDate->diff($dueDate)->format('%R%a'));
DateInterval::format的php文档中说:
%R -- "+" for positive interval, "-" for negative
%a -- the total count of days i the interval
如果结果有问题,请始终以天为单位返回6015,并使用正确的符号+或-。
我尝试为$currentdate
和$dueDate
使用不同的日期。
有人能告诉我为什么会有这种行为吗
谢谢我想你可能用错了 尝试: 编辑以添加 嗯。我刚刚运行了你的代码,它运行正常。问题一定是如何生成日期时间:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$daysDiff = intval($datetime1->diff($datetime2)->format('%R%a'));
echo $daysDiff
输出“2”您可以添加您正在使用的
$currentDate
和$dueDate
的值吗?$currentDate=new\DateTime()$dueDate是从DB获取的值,是有效的DateTime对象,并且始终,即使使用不同的值,也会得到6015天的差异。请在此处检查返回值:$datetime1->diff($datetime2)是一个具有某些属性的对象,例如:y表示年份m表示月份。。。总天数的天数,即使在此对象中,天数的值始终为6015@Oriam-我不知道你的意思,sorry@Oriam-见我的编辑。我认为,您生成的日期有问题。没有什么,我的朋友,这是一个事实,即此函数工作不正常,至少使用此参数“%a”,我将尝试使用年数、月数等,然后求和以获得总天数:(@Oriam-对我来说很好。你试过剪切和粘贴我上次编辑时添加的代码吗?
$interval = $currentDate->diff($dueDate);
echo $interval->format('%R%a');
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$daysDiff = intval($datetime1->diff($datetime2)->format('%R%a'));
echo $daysDiff