5

I need to generate two dates in the format YYYY-MM-DD example: 2010-06-09

The end date should be today and start date should be today - 30 days.

How can I generate these 2 dates in the above format?

Dharman
  • 30,962
  • 25
  • 85
  • 135
EzzDev
  • 9,900
  • 12
  • 35
  • 35

2 Answers2

31

for the last 30 days so end date is today and start date is today - 30 days

The strtotime is real friend:

echo date('Y-m-d', strtotime('today - 30 days'));
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

It's very simple with DateTime class. You can simply pass the string with the relative expression to the constuctor. When you need to get the date as a string in a specific format use format() method.

$endDate = new \DateTime();
$startDate = new \DateTime('-30 days');

// when you need to use them simple format as a string:
echo $endDate->format('Y-m-d');
echo $startDate->format('Y-m-d');
Dharman
  • 30,962
  • 25
  • 85
  • 135