Strtotime/86400 不起作用 (php)



我试图创建一个php脚本,该脚本从daterangepicker中获取日期,创建两个变量,然后在strtotime中转换。

然后,我将差值除以 86400 以返回天数,但不是返回 1(当我选择一天时(,而是返回 31。你知道我在哪里犯了错误吗?

$_POST['periode']=03/06/2018 00:00 - 04/06/2018 00:00时的示例

$datete=$_POST['periode'];
list($debut, $fin) = explode(" - ", "$datete", 2);
$debutTS=strtotime($debut);
$finTS=strtotime($fin);
$diff=($finTS-$debutTS)/86400

$debutTS返回1520294400,$finTS返回1522972800。$debutTS应该返回1527976800$finTs应该返回1528063200

知道吗?

你可以使用 PHP 的 DateTime-class 来实现这一点。它还可以为您计算差异,因此没有理由手动执行此操作。

$dates = explode(' - ' , $_POST['periode']);
// Create the first date from your date/time format
$from = DateTime::createFromFormat('d/m/Y H:i', $dates[0]);
// Create the second date from your date/time format
$to = DateTime::createFromFormat('d/m/Y H:i', $dates[1]);
// Get the difference
$interval = $from->diff($to);
// Get the difference in days (it's the %a token)
// No need to calculate anything manually
$diff = $interval->format('%a');
echo $diff;

演示:https://3v4l.org/HVukA

最新更新