Skip to content
Advertisement

Calculate the first day of next month and quarter from today in PHP

For calculating the first dayof next month from today:

$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));

How to calculate the first day of next quarter from today?

  • First quarter, Q1: 1 January – 31 March
  • Second quarter, Q2: 1 April – 30 June
  • Third quarter, Q3: 1 July – 30 September
  • Fourth quarter, Q4: 1 October – 31 December

Thank you!

Advertisement

Answer

There may be more elegant ways but this does the trick

$today = '2021-12-03';

$now = new DateTimeImmutable($today);
$nowMonth = (int) $now->format('m');
$yr = (int) $now->format('Y');

# Next quarter
echo 'Todays date is ' . $now->format('d/m/Y').PHP_EOL;
switch ($nowMonth){
    case 1:
    case 2:
    case 3:
        $quarter = new DateTimeImmutable("$yr/04/01");
        echo 'Next quarter is ' . $quarter->format('d/m/Y') . PHP_EOL;
        break;
    case 4:
    case 5:
    case 6:
        $quarter = new DateTimeImmutable("$yr/07/01");
        echo 'Next quarter is ' . $quarter->format('d/m/Y') . PHP_EOL;
        break;
    case 7:
    case 8:
    case 9:
        $quarter = new DateTimeImmutable("$yr/10/01");
        echo 'Next quarter is ' . $quarter->format('d/m/Y') . PHP_EOL;
        break;
    case 10:
    case 11:
    case 12:
        $yr++;
        $quarter = new DateTimeImmutable("$yr/01/01");
        echo 'Next quarter is ' . $quarter->format('d/m/Y') . PHP_EOL;
        break;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement