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?
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'));
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');