[英]Subtracting time PHP
I am having trouble with subtracting times in PHP. 我在用PHP减去时间时遇到麻烦。
My times are: 我的时间是:
$row['exp'] = 2017-03-31 $ row ['exp'] = 2017-03-31
and date('Ym-d') = 2017-03-10 (today) 和日期('Ym-d') = 2017-03-10(今天)
$datetime1 = strtotime($row['exp']);
$datetime2 = strtotime(date('Y-m-d'));
$secs = $datetime1 - $datetime2;// == <seconds between the two times>
$days = $secs / 86400;
echo $days;
My result is: 我的结果是:
36520.958333333333
36520.958333333333
Use the PHP DateTime class instead, it has built in functions, such as DateTime::diff, which seems to be what you're looking for. 改用PHP DateTime类,它具有内置的功能,例如DateTime :: diff,这似乎是您想要的。 The DateTime objects are much easier to work with too.
DateTime对象也更容易使用。
$today = new DateTime('today');
$date = new DateTime("2017-03-31");
echo $today->diff($date)->d; // 21
Just pass your $row['exp']
as the parameter of $date
instead, so you get 只需将
$row['exp']
传递为$date
的参数,即可获得
$date = new DateTime($row['exp']);
Although, I can't reproduce your issue with the given input you have, this live demo outputs "20.958333333333" (which is about right, you just need to ceil()
it). 尽管我无法使用给定的输入来重现您的问题,但此实时演示输出“ 20.958333333333”(大约正确,您只需要
ceil()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.