我有两个表格日期:
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中执行此操作?
当前回答
您可以始终使用以下函数,以年和月为单位返回年龄(即1年4个月)
function getAge($dob, $age_at_date)
{
$d1 = new DateTime($dob);
$d2 = new DateTime($age_at_date);
$age = $d2->diff($d1);
$years = $age->y;
$months = $age->m;
return $years.'.'.months;
}
或者如果希望在当前日期计算年龄,可以使用
function getAge($dob)
{
$d1 = new DateTime($dob);
$d2 = new DateTime(date());
$age = $d2->diff($d1);
$years = $age->y;
$months = $age->m;
return $years.'.'.months;
}
其他回答
查看以下链接。这是迄今为止我找到的最好的答案
function dateDiff ($d1, $d2) {
// Return the number of days between the two dates:
return round(abs(strtotime($d1) - strtotime($d2))/86400);
} // end function dateDiff
当你通过日期参数。函数使用PHP ABS()绝对值始终返回正数作为两者之间的天数日期。请记住,两个日期之间的天数不是包括两个日期。因此,如果您正在寻找天数由输入日期之间的所有日期表示,您需要向该函数的结果添加一(1)。例如,差异(由上述函数返回)2013-02-09和2013-02-14之间的值为5。但天数或日期范围2013-02-09-2013-02-14表示的日期为6。
http://www.bizinfosys.com/php/date-difference.html
使用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
一便士一英镑:我刚刚回顾了几个解决方案,所有这些方案都使用floor()提供了一个复杂的解决方案,然后四舍五入到26年12个月零2天的解决方案中,原本应该是25年11个月零20天!!!!
这是我对这个问题的看法:可能不优雅,可能编码不好,但如果不计算LEAP年份,则提供了更接近答案的答案,显然闰年可以编码为,但在这种情况下-正如其他人所说,也许您可以提供以下答案:我已经包含了所有测试条件和print_r,以便您可以更清楚地看到结果的构造:在这里,
//设置输入日期/变量::
$ISOstartDate = "1987-06-22";
$ISOtodaysDate = "2013-06-22";
//我们需要将ISO yyyy-mm-dd格式分解为yyyy-mm-d格式,如下所示:
$yDate[]=爆炸('-',$ISOstartDate);print_r($yDate);
$zDate[]=爆炸('-',$ISOtodaysDate);print_r($zDate);
// Lets Sort of the Years!
// Lets Sort out the difference in YEARS between startDate and todaysDate ::
$years = $zDate[0][0] - $yDate[0][0];
// We need to collaborate if the month = month = 0, is before or after the Years Anniversary ie 11 months 22 days or 0 months 10 days...
if ($months == 0 and $zDate[0][1] > $ydate[0][1]) {
$years = $years -1;
}
// TEST result
echo "\nCurrent years => ".$years;
// Lets Sort out the difference in MONTHS between startDate and todaysDate ::
$months = $zDate[0][1] - $yDate[0][1];
// TEST result
echo "\nCurrent months => ".$months;
// Now how many DAYS has there been - this assumes that there is NO LEAP years, so the calculation is APPROXIMATE not 100%
// Lets cross reference the startDates Month = how many days are there in each month IF m-m = 0 which is a years anniversary
// We will use a switch to check the number of days between each month so we can calculate days before and after the years anniversary
switch ($yDate[0][1]){
case 01: $monthDays = '31'; break; // Jan
case 02: $monthDays = '28'; break; // Feb
case 03: $monthDays = '31'; break; // Mar
case 04: $monthDays = '30'; break; // Apr
case 05: $monthDays = '31'; break; // May
case 06: $monthDays = '30'; break; // Jun
case 07: $monthDays = '31'; break; // Jul
case 08: $monthDays = '31'; break; // Aug
case 09: $monthDays = '30'; break; // Sept
case 10: $monthDays = '31'; break; // Oct
case 11: $monthDays = '30'; break; // Nov
case 12: $monthDays = '31'; break; // Dec
};
// TEST return
echo "\nDays in start month ".$yDate[0][1]." => ".$monthDays;
// Lets correct the problem with 0 Months - is it 11 months + days, or 0 months +days???
$days = $zDate[0][2] - $yDate[0][2] +$monthDays;
echo "\nCurrent days => ".$days."\n";
// Lets now Correct the months to being either 11 or 0 Months, depending upon being + or - the years Anniversary date
// At the same time build in error correction for Anniversary dates not being 1yr 0m 31d... see if ($days == $monthDays )
if($days < $monthDays && $months == 0)
{
$months = 11; // If Before the years anniversary date
}
else {
$months = 0; // If After the years anniversary date
$years = $years+1; // Add +1 to year
$days = $days-$monthDays; // Need to correct days to how many days after anniversary date
};
// Day correction for Anniversary dates
if ($days == $monthDays ) // if todays date = the Anniversary DATE! set days to ZERO
{
$days = 0; // days set toZERO so 1 years 0 months 0 days
};
echo "\nTherefore, the number of years/ months/ days/ \nbetween start and todays date::\n\n";
printf("%d years, %d months, %d days\n", $years, $months, $days);
最终结果是:26年零个月零天
这就是我在2013年6月22日做生意的时间——哎呦!
我有一些简单的逻辑:
<?php
per_days_diff('2011-12-12','2011-12-29')
function per_days_diff($start_date, $end_date) {
$per_days = 0;
$noOfWeek = 0;
$noOfWeekEnd = 0;
$highSeason=array("7", "8");
$current_date = strtotime($start_date);
$current_date += (24 * 3600);
$end_date = strtotime($end_date);
$seassion = (in_array(date('m', $current_date), $highSeason))?"2":"1";
$noOfdays = array('');
while ($current_date <= $end_date) {
if ($current_date <= $end_date) {
$date = date('N', $current_date);
array_push($noOfdays,$date);
$current_date = strtotime('+1 day', $current_date);
}
}
$finalDays = array_shift($noOfdays);
//print_r($noOfdays);
$weekFirst = array("week"=>array(),"weekEnd"=>array());
for($i = 0; $i < count($noOfdays); $i++)
{
if ($noOfdays[$i] == 1)
{
//echo "This is week";
//echo "<br/>";
if($noOfdays[$i+6]==7)
{
$noOfWeek++;
$i=$i+6;
}
else
{
$per_days++;
}
//array_push($weekFirst["week"],$day);
}
else if($noOfdays[$i]==5)
{
//echo "This is weekend";
//echo "<br/>";
if($noOfdays[$i+2] ==7)
{
$noOfWeekEnd++;
$i = $i+2;
}
else
{
$per_days++;
}
//echo "After weekend value:- ".$i;
//echo "<br/>";
}
else
{
$per_days++;
}
}
/*echo $noOfWeek;
echo "<br/>";
echo $noOfWeekEnd;
echo "<br/>";
print_r($per_days);
echo "<br/>";
print_r($weekFirst);
*/
$duration = array("weeks"=>$noOfWeek, "weekends"=>$noOfWeekEnd, "perDay"=>$per_days, "seassion"=>$seassion);
return $duration;
?>
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');