简体   繁体   English

PHP DateTime:获取与上一年相比的当前周/月/年间隔(开始-结束)

[英]PHP DateTime : get current week/month/year interval (start-end) compared with the previous year

Today is 2015-05-22 (to understand the example) 今天是2015年5月22日(了解示例)

I have to see some statistics taking values from a database. 我必须看到一些统计信息从数据库中获取值。 Initially I had to do these comparisons 最初我必须进行这些比较

  • this week compared with the previous week. 这个星期与前一周相比。 [ 2015-05-18 00:00:00 # 2015-05-24 23:59:59 ] - [ 2015-05-11 00:00:00 # 2015-05-17 00:00:00 ] [ 2015-05-18 00: 00:002015-05-24 23:59:59 ]-[ 2015-05-11 00:00:002015-05-17 00:00:00 ]
  • this month compared with the previous month. 这个月与上个月相比。 [ 2015-05-01 00:00:00 # 2015-05-31 23:59:59 ] - [ 2015-04-01 00:00:00 # 2015-04-30 23:59:59 ] [ 2015-05-01 00: 00:002015-05-31 23:59:59 ]-[ 2015-04-01 00: 00:002015-04-30 23:59:59 ]
  • this year compared with the previous year. 今年与上一年相比。 [ 2015-01-01 00:00:00 # 2015-12-31 23:59:59 ] - [ 2014-01-01 00:00:00 # 2014-12-31 23:59:59 ] [ 2015-01-01 00:00:002015-12-31 23:59:59 ]-[ 2014-01-01 00: 00:002014-12-31 23:59:59 ]

For all these controls i do this function that calculated all the various date intervals. 对于所有这些控件,我都执行此功能,该功能可以计算所有各种日期间隔。

function getIntervalDate(){

    $today = new DateTime();
    $appToday = new DateTime();

    $numDay = date("w");
    $numYear = (int)date("Y");

    switch($numDay){
        //monday
        case 1 : 
            $startWeek = $today->format('Y-m-d 00:00:00');
            $endWeek = $today->modify('next sunday')->format('Y-m-d 23:59:59');

            $startWeek2 = $today->modify('last monday -1 week')->format('Y-m-d 00:00:00');
            $endWeek2 = $today->modify('next sunday')->format('Y-m-d 00:00:00');

        break;
        default:
            $startWeek = $today->modify('last monday')->format('Y-m-d 00:00:00');
            $endWeek = $today->modify('next sunday')->format('Y-m-d 23:59:59');

            $startWeek2 = $today->modify('last monday -1 week')->format('Y-m-d 00:00:00');
            $endWeek2 = $today->modify('next sunday')->format('Y-m-d 00:00:00');

        break;
    }

    $startMonth = $today->modify('first day of this month')->format('Y-m-d H:i:s');
    $endMonth = $today->modify('last day of this month')->format('Y-m-d 23:59:59');

    $today1 = $appToday;

    $startMonth2 = $today1->modify('first day of previous month')->format('Y-m-d H:i:s');
    $endMonth2 = $today1->modify('last day of this month')->format('Y-m-d 23:59:59');

    $today2 = $appToday;

    $startYear = $today2->modify('first day of January '.$numYear)->format('Y-m-d 00:00:00');
    $endYear = $today2->modify('last day of December '.$numYear)->format('Y-m-d 23:59:59');

    $today3 = $appToday;

    $startYear2 = $today3->modify('first day of January '.($numYear-1))->format('Y-m-d 00:00:00');
    $endYear2 = $today3->modify('last day of December '.($numYear-1))->format('Y-m-d 23:59:59');

    return array(

        "startWeek" => $startWeek,
        "endWeek" => $endWeek,
        "startWeek2" => $startWeek2,
        "endWeek2" => $endWeek2,

        "startMonth" => $startMonth,
        "endMonth" => $endMonth,
        "startMonth2" => $startMonth2,
        "endMonth2" => $endMonth2,

        "startYear" => $startYear,
        "endYear" => $endYear,
        "startYear2" => $startYear2,
        "endYear2" => $endYear2

    );

}

在此处输入图片说明

Everything works perfectly. 一切正常。 But now i have to change the various comparisons. 但是现在我必须更改各种比较。 I have to always compare the current week / month / year interval but with the week / month / year in the previous year (in the case of the year there are no problems, the code remains the same) 我必须始终比较当前的周/月/年间隔,但要与上一年的周/月/年进行比较(如果年份没有问题,则代码保持不变)

I did some tests but i failed. 我做了一些测试,但失败了。 I used mostly the ->modify('-1 year') but with not a perfect result. 我主要使用->modify('-1 year')但效果并不理想。

Based on your discussion above here is the solution of your problem: 根据上面的讨论,这里是您的问题的解决方案:

$startWeek->modify("-1 year")->modify('monday')->format('Ymd H:i:s');

Here is the test. 这是测试。

$today = DateTime::createFromFormat('Y-m-d H:i:s', '2015-05-18 00:00:00');
$startWeekPrevYear=$today->modify('-1 year')->modify('monday');

echo $startWeekPrevYear->format('Y-m-d H:i:s');//outputs 2014-05-19 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM