获取字符串中的日期剩余天数

时间:2022-08-25 18:51:24

I have a string $newAfter that outputs like Wednesday 9th of March 2016 11:59:59 PM.

我有一个字符串$ newAfter输出像2016年3月9日星期三晚上11:59:59。

I use this code to calculate the days left:

我使用此代码计算剩余的天数:

$daysleft = floor(($newAfter - time()) / 86400);

this is the result I get:

这是我得到的结果:

-16559

I want the days remaining up leading up to the date in string.

我希望剩下的日子一直持续到字符串中的日期。

1 个解决方案

#1


Of course you can't subtract days from that format string, you'll need to subtract them as integers of use DateTime objects:

当然你不能从那个格式字符串中减去天数,你需要将它们减去为使用DateTime对象的整数:

$notAfter = 'Thu, 28 Apr 2016 03:22:56 +0200';
$today = new DateTime;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $notAfter);
$diff = $date->diff($today);
echo "{$diff->days} days left.";

Demo

#1


Of course you can't subtract days from that format string, you'll need to subtract them as integers of use DateTime objects:

当然你不能从那个格式字符串中减去天数,你需要将它们减去为使用DateTime对象的整数:

$notAfter = 'Thu, 28 Apr 2016 03:22:56 +0200';
$today = new DateTime;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $notAfter);
$diff = $date->diff($today);
echo "{$diff->days} days left.";

Demo