我有两个表格日期:
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中执行此操作?
当前回答
一便士一英镑:我刚刚回顾了几个解决方案,所有这些方案都使用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日做生意的时间——哎呦!
其他回答
一便士一英镑:我刚刚回顾了几个解决方案,所有这些方案都使用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日做生意的时间——哎呦!
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');
我投票支持jurka的答案,因为这是我最喜欢的,但我有一个pre-php.5.3版本。。。
我发现自己在解决一个类似的问题——这就是我最初如何回答这个问题——但只是需要时间上的差异。但我的函数也很好地解决了这个问题,而且我自己的库中没有任何地方可以将它保存在不会丢失和遗忘的地方,所以……希望这对某人有用。
/**
*
* @param DateTime $oDate1
* @param DateTime $oDate2
* @return array
*/
function date_diff_array(DateTime $oDate1, DateTime $oDate2) {
$aIntervals = array(
'year' => 0,
'month' => 0,
'week' => 0,
'day' => 0,
'hour' => 0,
'minute' => 0,
'second' => 0,
);
foreach($aIntervals as $sInterval => &$iInterval) {
while($oDate1 <= $oDate2){
$oDate1->modify('+1 ' . $sInterval);
if ($oDate1 > $oDate2) {
$oDate1->modify('-1 ' . $sInterval);
break;
} else {
$iInterval++;
}
}
}
return $aIntervals;
}
测试:
$oDate = new DateTime();
$oDate->modify('+111402189 seconds');
var_dump($oDate);
var_dump(date_diff_array(new DateTime(), $oDate));
结果是:
object(DateTime)[2]
public 'date' => string '2014-04-29 18:52:51' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'America/New_York' (length=16)
array
'year' => int 3
'month' => int 6
'week' => int 1
'day' => int 4
'hour' => int 9
'minute' => int 3
'second' => int 8
我从这里得到了最初的想法,我对其进行了修改以供使用(我希望我的修改也会显示在该页面上)。
通过从$aIntervals数组中删除不需要的间隔(例如“周”),或者添加$aExclude参数,或者在输出字符串时过滤掉它们,可以非常容易地删除它们。
$date1 = date_create('2007-03-24');
$date2 = date_create('2009-06-26');
$interval = date_diff($date1, $date2);
echo "difference : " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
$date = '2012.11.13';
$dateOfReturn = '2017.10.31';
$substract = str_replace('.', '-', $date);
$substract2 = str_replace('.', '-', $dateOfReturn);
$date1 = $substract;
$date2 = $substract2;
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
echo $diff = (($year2 - $year1) * 12) + ($month2 - $month1);