我们怎样才能获得时间戳日期差异并使用php显示(d.m.Y)格式?

时间:2017-05-22 06:48:35

标签: php

如何使用php获取日期差异?

我尝试了一些代码,但显示错误:date_format() expects parameter 1 to be DateTimeInterface, object given

这是我试过的代码

$checkin_from = 1502143200000
$reserve_to = 1502229600000
$days       = round(abs(strtotime(date_format($checkin_from , 'd.m.Y')) - strtotime(date_format($reserve_to, 'd.m.Y'))) / 86400);

4 个答案:

答案 0 :(得分:0)

您使用毫秒而不是秒,首先必须将它们除以1000,以将其转换为有效的timestamp秒。

Try this code snippet here

<?php
ini_set('display_errors', 1);

$checkin_from = 1502143200000/1000;
$reserve_to = 1502229600000/1000;

$dateObj1= new DateTime();
$dateObj1->setTimestamp($checkin_from);

$dateObj2= new DateTime();
$dateObj2->setTimestamp($reserve_to);

$result=$dateObj1->diff($dateObj2);
print_r($result->d);   

答案 1 :(得分:-1)

使用以下代码:

<?php
$checkin_from = '1502143200000';
$reserve_to = '1502229600000';
$datediff = $reserve_to - $checkin_from;

echo date('Y-m-d',$checkin_from)."\n";
echo date('Y-m-d',$reserve_to)."\n";
echo floor($datediff / (60 * 60 * 24));

<强>输出

1935-04-17
1938-01-11
Total Days: 1000

演示: Click Here

答案 2 :(得分:-1)

您可以尝试以下示例。

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

或者你可以在下面试试。

$date1 = "2007-03-24";
$date2 = "2009-06-26";
$datediff = $date2 - $date1;

echo floor($datediff / (60 * 60 * 24));

答案 3 :(得分:-1)

<?php
$checkin_from = 1502143200000;
$reserve_to = 1502229600000;
$diff = $checkin_from - $reserve_to;
$days = abs($diff/86400*1000);
print $days;

86400是一天中的秒数