简体   繁体   English

无法获得两个日期之间的时差

[英]Unable to get time difference between two dates

I am trying to find the time difference between an expiry date and today's date, but my code doesn't work.我试图找到到期日期和今天日期之间的时差,但我的代码不起作用。

$time_one=DateTime::createFromFormat('d/m/Y', get_field('expirydate'));
$time_two=new DateTime();

$timeleft = $time_one->diff($time_two);
echo $timeleft;

The diff method returns you an DateInterval object. diff方法返回一个DateInterval对象。 You must format the output in order to see how many days, hours, minutes, seconds remained:您必须格式化输出以查看剩余的天数、小时数、分钟数、秒数:

$dateTimeInTheFuture = DateTime::createFromFormat('d/m/Y', get_field('expirydate'));

$dateInterval = $dateTimeInTheFuture->diff(new DateTime());

echo 'Time remained until expire: ' . $dateInterval->format('%d days, %h hours, %i minutes, %s seconds');

Read more about DateInterval.format method阅读有关DateInterval.format方法的更多信息

Try:尝试:

echo $timeleft->format('%a days');

So when you apply diff function to DateTime object -> you receive a DateInterval object.因此,当您将diff函数应用于 DateTime 对象时 -> 您会收到一个DateInterval对象。 To access its properties about total difference between dates you should apply method called format.要访问有关日期之间总差异的属性,您应该应用称为格式的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM