1

I've used PHP code to get next Sunday's date. But I want to get next Sunday's date based on the dynamic date like-

$y = '2018';
$m = '09';
$d = '16';
$dts = $y.'-'.$m.'-'.$d;
$nextDate = date($dts,strtotime('next Sunday'));
echo $nextDate;

I am unable to get next Sunday date.

I would appreciate any help from you guys. Thanks in advance.

Jagdeesh Kumar
  • 1,640
  • 2
  • 13
  • 29

2 Answers2

2

Or you can use DateTime

$nextDate = (new DateTime())->modify('next Sunday')->format('Y-m-d');

And likewise you can put any valid date in the constructor call.

$nextDate = (new DateTime($dts))->modify('next Sunday')->format('Y-m-d');

Then it will modify it from that starting date.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
1

Using PHP's strtotime with a value of "next Sunday" is correct. The problem is that the format values you're passing to date are the literal values of "2018","09", and "16", rather than the format string (it looks like you want "Y-m-d").

This will get you the date for next Sunday from the current date:

$nextDate = date('Y-m-d',strtotime('next Sunday'));

If you want to get the next Sunday from any given date, you simply need to pass the date as a second argument to strtotime:

$y = '2018';
$m = '09';
$d = '16';
$dts = $y.'-'.$m.'-'.$d;
$nextDate = date('Y-m-d',strtotime('next Sunday', strtotime($dts)));
echo $nextDate;
RToyo
  • 2,877
  • 1
  • 15
  • 22