2015-05-09 85 views
0

我已指定变量$ DATE1两个日期和$ date2的.. 这里是代码的区别..如何计算两个日期

if (isset($_POST['check_in'])) 
{ 
$date1=date('Y-m-d', strtotime($_POST['check_in'])); 
} 
if (isset($_POST['check_in'])) 
{ 
$date2=date('Y-m-d', strtotime($_POST['check_out'])); 
} 

例如,如果date1="2015-05-21"date2="2015-05-23"。我想要的差日期2

回答

0

这里你去:

https://php.net/manual/en/datetime.diff.php

与VA代码有趣的例子。

这里有一个我喜欢的:

<?php 
$datetime1 = date_create('2015-05-21'); 
$datetime2 = date_create('2015-05-23'); 
$interval = date_diff($datetime1, $datetime2); 
echo $interval->format('%R%a days'); 
?> 

我希望这有助于:)

1

使用DateTime类。尝试用 -

$date1=new DateTime("2015-05-21"); 
$date2=new DateTime("2015-05-23"); 

$interval = $date1->diff($date2); 
echo $interval->format('%R%a days'); 

输出

+2 days 

DateTime()

0

由于strtotime返回unixtime,以秒为单位的差异可以通过简单地减去其他的一个strtotime计算:

$seconds = strtotime($_POST['check_out']) - strtotime($_POST['check_in']); 

然后找到天数:

$days = $seconds/60/60/24;