get startdate and enddate for current quarter php

前端 未结 19 1630
悲哀的现实
悲哀的现实 2020-12-29 10:58

I am trying to set a start date and end date by the quarter.

For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, qu

19条回答
  •  情歌与酒
    2020-12-29 11:25

    Just wanted to point SynaTree's solution doesn't work for every last 3rd month in quarter.

    Here's a modified solution using DateTime.

    $now = new DateTimeImmutable();
    $offset = ($now->format('n') - 1) % 3;
    $start = $now->modify("first day of -{$offset} month midnight");
    $endExcluded = $start->modify("+3 month");
    $endIncluded = $start->modify("+3 month -1 second");
    

    $endExcluded works well when for DatePeriod loops, where the end date is excluded when time is 00:00:00.

提交回复
热议问题