I want to get the DateTime of one week ago to use it in a query. So I've done:
$date = new DateTime();
$date->sub(new DateInterval('P7D'));
$date = $date->format('Y-m-d H:i:s');
But for some reason this is not working right. Any idea of what I'm doing wrong?
DateTime::sub()
returns a new DateTime object. It doesn't change the original object at all.
$date = new DateTime();
$newdate = $date->sub(new DateInterval('P7D'));
$date = $newdate->format('Y-m-d H:i:s');
Demo
If you want to do this a little simpler:
$date = new DateTime('-1 week');
$date = $date->format('Y-m-d H:i:s');
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With