简体   繁体   English

strtotime减法不起作用

[英]strtotime subtraction not working

I have this PHP code: 我有这个PHP代码:

echo "time30: ".$time30.'<br />';

echo "time: ".$time.'<br />';

echo date("s",strtotime($time) - strtotime($time30));

Which is returning: 哪个回来了:

time30: 15/09/2015 20:27:16
time: 15/09/2015 21:08:41
00

Why is it not returning the difference instead of 00 ?! 为什么它没有返回差异而不是00

Subtracting 2 timestamps doesn't get you a timestamp. 减去2个时间戳并不能为您提供时间戳。 It gets you the difference between them. 它可以让你了解它们之间的区别 You are then trying to read that difference as a timestamp (or an exact moment in time), and it just to happens to have a "seconds" value of 0 . 然后,您尝试将该差异读作时间戳(或精确的时刻),并且恰好将“秒”值设为0

If you want the number of seconds between these two times, then you just need to do: 如果你想要这两次之间的秒数 ,那么你只需要做:

echo strtotime($time) - strtotime($time30);

PS 15/09/2015 20:27:16 is not a format that strtotime recognizes. PS 15/09/2015 20:27:16不是strtotime认可的格式。 I suggest you use DateTime objects for this task. 我建议你使用DateTime对象来完成这项任务。

$time = DateTime::createFromFormat('d/m/Y H:i:s', '15/09/2015 21:08:41');
$time30 = DateTime::createFromFormat('d/m/Y H:i:s', '15/09/2015 20:27:16');

$diff = $time->diff($time30);

$minutes = ($diff->days*24*60) + ($diff->h*60) + ($diff->i);
$seconds = ($minutes*60) + $diff->s;

echo $seconds;  // 2485 (which is 41 minutes, 25 seconds)

instead of using 而不是使用

$time30= '15/09/2015 20:27:16';
$time= '15/09/2015 21:08:41';

use 采用

$time30= '2015-09-15 20:27:16';
$time= '2015-09-15 21:08:41';

Format has to be correct http://php.net/manual/en/function.strtotime.php 格式必须正确http://php.net/manual/en/function.strtotime.php

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

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