计算PHP中日期之间的绝对差异

我正在尝试创建一个函数,我可以用它来查找 2 个日期时间之间的差异,但我希望能够返回每个日期时间的绝对值。

说差别是15分20秒,我希望能输出15分20秒,或者920秒。这也将扩展到天、月、年。因此,2 天 12 分钟可能是 2 天、48 小时、2892 分钟或 173520 秒。

我最初是从我自己的代码开始的,但想在考虑天、月和年时考虑适当的差异,所以我改为使用 DateDiff,我现在发现它不能像我想要的那样工作。

这是一个代码示例


$dtmNow = new \DateTime();

$authExpireDtm = new \DateTime();

$authExpireDtm->modify('+15 minutes');


echo friendlyDtmDiff($dtmNow->format('Y-m-d H:i:s'), $authExpireDtm->format('Y-m-d H:i:s'), 'l', 's');

我想输出900 seconds,但输出0 seconds,这是该对象$interval有


DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 15 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) 

如您所见0,该s项目下没有任何内容 ( ) ,因此当然我的函数正在返回0 seconds. 我想知道如何让它返回900秒数。我知道比较时间(小时、分钟、秒)很容易,但我想在转换天、月、年时得到准确的结果。


所以,如果我把2020-02-28 00:00:00和2020-03-01 00:00:00它将返回172800 seconds或2880 minutes,但如果我把2019-02-28 00:00:00和2019-03-01 00:00:00它将返回86400 seconds或1440 minutes


有没有人能够指出我正确的方向来实现我所追求的结果


繁花如伊
浏览 183回答 1
1回答

凤凰求蛊

这是一个可以完成您想要的 99% 的函数(它不实现四舍五入 when length != 'l',也不会删除sfrom eg 1 years)。0 值不会输出,尽管您可以添加一个参数来控制它。function friendlyDtmDiff($date1, $date2, $length = '', $format = '') {&nbsp; &nbsp; // Create DateTime for diff()&nbsp; &nbsp; $dt1 = new \DateTime($date1);&nbsp; &nbsp; $dt2 = new \DateTime($date2);&nbsp; &nbsp; // Create intervals&nbsp; &nbsp; if ($dt1 < $dt2) {&nbsp; &nbsp; &nbsp; &nbsp; $sign = '';&nbsp; &nbsp; &nbsp; &nbsp; $interval = $dt1->diff($dt2);&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; $sign = '-';&nbsp; &nbsp; &nbsp; &nbsp; $interval = $dt2->diff($dt1);&nbsp; &nbsp; }&nbsp; &nbsp; // Output format (minimum 2 digits for upper case formats)&nbsp; &nbsp; $of = ($format < 'a') ? '%02d' : '%d';&nbsp; &nbsp; // generate output using an array of terms to be imploded&nbsp; &nbsp; $output = array();&nbsp; &nbsp; // create time components&nbsp; &nbsp; switch ($format) {&nbsp; &nbsp; &nbsp; &nbsp; case 'Y':&nbsp; &nbsp; &nbsp; &nbsp; case 'y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $years = $interval->y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($years) $output[] = sprintf("$of years", $years);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($length != 'l') break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $interval->y = 0;&nbsp; &nbsp; &nbsp; &nbsp; case 'M':&nbsp; &nbsp; &nbsp; &nbsp; case 'm':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $months = $interval->y * 12 + $interval->m;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($months) $output[] = sprintf("$of months", $months);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($length != 'l') break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $interval->m = $interval->y = 0;&nbsp; &nbsp; &nbsp; &nbsp; case 'D':&nbsp; &nbsp; &nbsp; &nbsp; case 'd':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $days = ($interval->y * 12 + $interval->m) * 30 + $interval->d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($days) $output[] = sprintf("$of days", $days);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($length != 'l') break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $interval->d = $interval->m = $interval->y = 0;&nbsp; &nbsp; &nbsp; &nbsp; case 'H':&nbsp; &nbsp; &nbsp; &nbsp; case 'h':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hours = (($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($hours) $output[] = sprintf("$of hours", $hours);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($length != 'l') break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $interval->h = $interval->d = $interval->m = $interval->y = 0;&nbsp; &nbsp; &nbsp; &nbsp; case 'I':&nbsp; &nbsp; &nbsp; &nbsp; case 'i':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $minutes = ((($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h) * 60 + $interval->i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($minutes) $output[] = sprintf("$of minutes", $minutes);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($length != 'l') break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $interval->i = $interval->h = $interval->d = $interval->m = $interval->y = 0;&nbsp; &nbsp; &nbsp; &nbsp; case 'S':&nbsp; &nbsp; &nbsp; &nbsp; case 's':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $seconds = (((($interval->y * 12 + $interval->m) * 30 + $interval->d) * 24 + $interval->h) * 60 + $interval->i) * 60 + $interval->s;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($seconds) $output[] = sprintf("$of seconds", $seconds);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'Invalid format';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; // put the output string together&nbsp; &nbsp; $last = array_pop($output);&nbsp; &nbsp; return $sign . (count($output) ? implode(', ', $output) . ' and ' : '') . $last;}用法示例:echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:00:56', '', 'h') . PHP_EOL;echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:00:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2020-02-28 00:00:00', '2020-03-01 12:08:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'y') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'm') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'd') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'h') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 'i') . PHP_EOL;echo friendlyDtmDiff('2018-12-28 00:00:00', '2020-04-11 04:08:56', 'l', 's') . PHP_EOL;输出:60 hours60 hours and 56 seconds60 hours, 8 minutes and 56 seconds1 years, 3 months, 14 days, 4 hours, 8 minutes and 56 seconds15 months, 14 days, 4 hours, 8 minutes and 56 seconds464 days, 4 hours, 8 minutes and 56 seconds11140 hours, 8 minutes and 56 seconds668408 minutes and 56 seconds40104536 seconds3v4l.org 上的演示
打开App,查看更多内容
随时随地看视频慕课网APP