我有两个表格日期:
Start Date: 2007-03-24
End Date: 2009-06-26
现在我需要通过以下形式找到这两者之间的区别:
2 years, 3 months and 2 days
如何在PHP中执行此操作?
我有两个表格日期:
Start Date: 2007-03-24
End Date: 2009-06-26
现在我需要通过以下形式找到这两者之间的区别:
2 years, 3 months and 2 days
如何在PHP中执行此操作?
当前回答
我在下面的页面上找到了您的文章,其中包含了许多PHP日期时间计算的参考。
使用PHP计算两个日期(和时间)之间的差异。下一页提供了一系列不同的方法(共7种),用于使用PHP执行日期/时间计算,以确定两个日期之间的时间差(小时、弹药)、天、月或年。
请参阅PHP日期时间–计算两个日期之间差值的7种方法。
其他回答
使用date_diff()尝试这个非常简单的答案,这是经过测试的。
$date1 = date_create("2017-11-27");
$date2 = date_create("2018-12-29");
$diff=date_diff($date1,$date2);
$months = $diff->format("%m months");
$years = $diff->format("%y years");
$days = $diff->format("%d days");
echo $years .' '.$months.' '.$days;
输出为:
1 years 1 months 2 days
我在PHP5.2中遇到了同样的问题,并用MySQL解决了这个问题。可能并不是你想要的,但这会奏效,并返回天数:
$datediff_q = $dbh->prepare("SELECT DATEDIFF(:date2, :date1)");
$datediff_q->bindValue(':date1', '2007-03-24', PDO::PARAM_STR);
$datediff_q->bindValue(':date2', '2009-06-26', PDO::PARAM_STR);
$datediff = ($datediff_q->execute()) ? $datediff_q->fetchColumn(0) : false;
此处有更多信息http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
function showTime($time){
$start = strtotime($time);
$end = strtotime(date("Y-m-d H:i:s"));
$minutes = ($end - $start)/60;
// years
if(($minutes / (60*24*365)) > 1){
$years = floor($minutes/(60*24*365));
return "From $years year( s ) ago";
}
// monthes
if(($minutes / (60*24*30)) > 1){
$monthes = floor($minutes/(60*24*30));
return "From $monthes monthe( s ) ago";
}
// days
if(($minutes / (60*24)) > 1){
$days = floor($minutes/(60*24));
return "From $days day( s ) ago";
}
// hours
if(($minutes / 60) > 1){
$hours = floor($minutes/60);
return "From $hours hour( s ) ago";
}
// minutes
if($minutes > 1){
$minutes = floor($minutes);
return "From $minutes minute( s ) ago";
}
}
echo showTime('2022-05-05 21:33:00');
这是可运行的代码
$date1 = date_create('2007-03-24');
$date2 = date_create('2009-06-26');
$diff1 = date_diff($date1,$date2);
$daysdiff = $diff1->format("%R%a");
$daysdiff = abs($daysdiff);
由于每个人都在发布代码示例,这里有另一个版本。
我想要一个函数来显示从秒到年的差异(仅一个单位)。对于超过1天的时段,我希望它在午夜滚动(周一上午10点到周三上午9点是2天前,而不是1天前)。对于超过一个月的时间段,我希望滚动在当月的同一天(包括30/31天的月份和闰年)。
这就是我想到的:
/**
* Returns how long ago something happened in the past, showing it
* as n seconds / minutes / hours / days / weeks / months / years ago.
*
* For periods over a day, it rolls over at midnight (so doesn't depend
* on current time of day), and it correctly accounts for month-lengths
* and leap-years (months and years rollover on current day of month).
*
* $param string $timestamp in DateTime format
* $return string description of interval
*/
function ago($timestamp)
{
$then = date_create($timestamp);
// for anything over 1 day, make it rollover on midnight
$today = date_create('tomorrow'); // ie end of today
$diff = date_diff($then, $today);
if ($diff->y > 0) return $diff->y.' year'.($diff->y>1?'s':'').' ago';
if ($diff->m > 0) return $diff->m.' month'.($diff->m>1?'s':'').' ago';
$diffW = floor($diff->d / 7);
if ($diffW > 0) return $diffW.' week'.($diffW>1?'s':'').' ago';
if ($diff->d > 1) return $diff->d.' day'.($diff->d>1?'s':'').' ago';
// for anything less than 1 day, base it off 'now'
$now = date_create();
$diff = date_diff($then, $now);
if ($diff->d > 0) return 'yesterday';
if ($diff->h > 0) return $diff->h.' hour'.($diff->h>1?'s':'').' ago';
if ($diff->i > 0) return $diff->i.' minute'.($diff->i>1?'s':'').' ago';
return $diff->s.' second'.($diff->s==1?'':'s').' ago';
}