Home > database >  Is there a way to get the x-th day of the next month in PHP?
Is there a way to get the x-th day of the next month in PHP?

Time:01-16

I need to get the 4th day of the next month in PHP. Should be able to generate the date using strtotime()

I followed https://www.php.net/manual/en/datetime.formats.relative.php

tried strtotime('first day of next month 4 days') but it returns first day of next month always because of the precendence I think.

CodePudding user response:

strtotime() works with relative date and time.

Use DateTime() object

$date = new DateTime();
$date->modify('first day of next month');
$date->modify(' 3 days');
echo $date->format('Y-m-d');

Output:

2023-02-04

And if you want to change days, you could use variable:

$date = new DateTime();
$wantedDays = 3;
$date->modify('first day of next month');
$date->modify(' '.$wantedDays.' days');
echo $date->format('Y-m-d');

CodePudding user response:

Here is a code you can try. It is my hope that this will work for you

<?php
$d1 = date("Y-m-d", strtotime('first day of next month'));
echo date('Y-m-d', strtotime(' 3 days', strtotime($d1)));

Thanks.

  • Related