我想找到两个日期之间的区别。 它很简单,不显示任何内容,显示日期差异的空白页面。
下面是我正在使用的代码。
<?
$sql = $Db1->query("SELECT * FROM table LIMIT 5");
while($row = $Db1->fetch_array($sql)) {
$datetime1 = date("m-d-Y",mktime(0,0,$row['rec'],1,1,1970));
$datetime2 = date("m-d-Y",mktime(0,0,$row['send'],1,1,1970));
echo "$datetime1";
echo "$datetime2";
$diff=date_diff($datetime1,$datetime2);
echo $diff->format("%R%a days");
echo "$diff";
}
?>
答案 0 :(得分:0)
我创建了一个我用过的功能,你可以根据自己的要求改变它。 如果你想找到不同的当前日期和给定日期,请将日期留空。
function dayspassed($dategiven, $dategiven2){
if(empty($dategiven2)){
$currentdate = date('Y-m-d');
}else{
$currentdate = $dategiven2;
}
$currentdate = strtotime($currentdate);
$dategiven = explode(" ", $dategiven);
$dategiven = $dategiven['0'];
$yourdate = strtotime($dategiven);
$datediff = $yourdate - $currentdate;
return $difference = floor($datediff/(60*60*24));
}
答案 1 :(得分:0)
使用此功能
$date1 = $row['rec'];
$date2 = $row['send'];
$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);
答案 2 :(得分:0)
我有以下功能:
function get_date_difference_in_days($timestamp1, $timestamp2) {
$inputSeconds = abs($timestamp1 - $timestamp2);
$days = floor($inputSeconds / (24 * 60 * 60));
return $days;
}
答案 3 :(得分:0)
您的代码存在的问题是date_diff与datetime对象一起使用,并且您将参数作为字符串传递而不是作为日期时间对象传递。
查看docs清楚地说明date_diff是DateTime :: diff()的别名函数。
所以解决方案是:
$row = array("rec" => "2014-12-24", "send" => "2014-12-20");
$datetime1 = DateTime::createFromFormat("Y-m-d", $row["rec"]);
$datetime2 = DateTime::createFromFormat("Y-m-d", $row["send"]);
echo $datetime1->format("Y-m-d");
echo $datetime2->format("Y-m-d");
$diff = $datetime1->diff($datetime2);
echo $diff->format("%r%a");