我尝试创建一个从daterangepicker获取日期的php脚本,创建两个变量然后在strtotime中转换。
然后我将差值除以86400以返回天数,但不是返回一天(当我选择一天时),它返回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
有什么想法吗?
答案 0 :(得分:1)
您可以使用PHP的DateTime - 类来实现此目的。它也可以为您计算差异,因此没有理由手动执行此操作。
$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;