如何计算两天之间的差异作为格式化字符串? [英] How to calculate the difference between two days as a formatted string?

查看:94
本文介绍了如何计算两天之间的差异作为格式化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是到目前为止我得到的:

Here's what I've got so far:

/**
 * Parse a duration between 2 date/times in seconds
 * and to convert that duration into a formatted string
 *
 * @param integer $time_start start time in seconds
 * @param integer $time_end   end time in seconds
 * @param string  $format     like the php strftime formatting uses %y %m %w %d %h or %i.
 * @param boolean $chop       chop off sections that have 0 values
 */
public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) {

        if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);

        list($year_start,$month_start,$day_start) = explode('-',date('Y-m-d',$time_start));
        list($year_end,$month_end,$day_end) = explode('-',date('Y-m-d',$time_end));

        $years = $year_end - $year_start;
        $months = $month_end - $month_start;
        $days = $day_start - $day_end;
        $weeks = 0;
        $hours = 0;
        $mins = 0;
        $secs = 0;

        if(mktime(0,0,0,$month_end,$day_end) < mktime(0,0,0,$month_start,$day_start)) {
            $years -= 1;
        }
        if($days < 0) {
            $months -= 1;
            $days += 30; // this is an approximation...not sure how to figure this out
        }
        if($months < 0) $months += 12;
        if(strpos($format, '%y')===false) {
            $months += $years * 12;
        }
        if(strpos($format, '%w')!==false) {
            $weeks = floor($days/7);
            $days %= 7;
        }
        echo date('Y-m-d',$time_start).' to '.date('Y-m-d',$time_end).": {$years}y {$months}m {$weeks}w {$days}d<br/>";
}

(不完整的不准确)

我似乎数学不正确。由于leap年和月份的长短不同,天真地将其拆分是行不通的。

I can't seem to get the math right. Naively dividing it out won't work because of leap years and differing lengths of months.

逻辑也需要根据格式字符串进行更改。例如,将格式字符串%y年%m月%d日传递给2010年2月4日至2011年6月28日(作为unix时间戳),应输出 1年4个月24天,但是如果省略了%y year ,则该月需要增加12个月,即输出应该是 16个月24天

The logic also needs to change depending on the format string. For example, passing 04-Feb-2010 to 28-Jun-2011 (as unix timestamps) with format string %y year %m month %d day should output 1 year 4 month 24 day but if %y year is omitted then it needs to add 12 months to the month, i.e., output should be 16 month 24 day.

也应该处理时间...但是我还不知道

Should handle times too...but I haven't got to that yet.

这些 date_diff 解决方案处理。而且我不知道如何将其破解到 date_diff 中,因此这对我来说不是一个真正的解决方案。

None of these date_diff solutions handle weeks. And I don't know how I could hack it into date_diff, so that's not really a solution for me.

此外, $ diff-> format 不能满足我的要求……如果省略了较大的单位,则无法给出总的月份和天数。示例:

Furthermore, $diff->format doesn't do what I asked...to give the total months and days if "bigger units" are omitted. Example:

>>> $start = new DateTime('04-Feb-2010')
>>> $end = new DateTime('28-Jun-2011')
>>> $diff = $start->diff($end)
>>> $diff->format('%m months, %d days')
'4 months, 24 days'


如我前面所述,

应为 16个月,24天。在您完全理解问题之前,请不要这么快就结束我的问题。

Should be 16 months, 24 days, as I stated earlier. Please stop being so quick to close my question as a dupe before you understand it fully. If the solutions to other questions can be tweaked to solve this, fine, but please explain how, because I don't get it.

要清楚,

要清楚,


  • 如果省略了%y ,则年份应在月份中滚动

  • 如果省略%m ,则应将月份数为天

  • 如果省略%w ,如果省略%h ,则应将周数缩短为几天

  • 如果将%m 省略,则应将分钟数转换为秒数

  • / ul>

    如果省略了较小的单位,则可以在有意义的地方舍入下一个最大的单位。

If "smaller units" are omitted, the next biggest unit can be rounded or floored where it makes sense.

推荐答案

我不希望大家接受答案,但这是如何使date_diff做几周。

I don't expect an accepted answer, but here is how to get date_diff to do weeks.

<?php

$january = new DateTime('2010-01-01');
$february = new DateTime('2011-02-20 3:35:28');
$interval = $february->diff($january);

$parts = $interval->format('%y %m %d %h %i %s %a');

$weeks = 0;
list($years, $months, $days, $hours, $minutes, $seconds, $total_days) = explode(' ', $parts);

if ($days >= 7) {
    $weeks = (int)($days / 7);
    $days  %= 7;
}

echo "$years years, $months months, $weeks weeks, $days days, $hours hours, $minutes minutes $seconds seconds";
// 1 years, 1 months, 2 weeks, 5 days, 3 hours, 35 minutes 28 seconds

也许您可以将其集成到函数中,以进行翻转和处理用户指定的格式。

Maybe with that you can integrate it into your function to do the rolling over and handling the user given format.

如果未提供较大的单位,您可以从最大的单元开始,然后将其应用到下一个较小的单元。 (即1年1个月,没有年份应将12加回月份)。如果格式中不包含 month,则可以使用总天数来处理月份具有不同天数的事实。

If the bigger units aren't given, you can start from the largest unit and apply them back to the next smaller unit. (i.e. 1 year 1 month with no years should add 12 back to months). If "month" isn't included in the format, then you can use the total days to handle the fact that months have different numbers of days.

这篇关于如何计算两天之间的差异作为格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆