Home » Php » PHP Date Function Seven days previous

PHP Date Function Seven days previous

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to use PHP’s Date Function to get the date of 7 days earlier in YYYY-MM-DD format.

date('Y-m-d');

when i try

date('Y-m-d-7');

i get an error

How to&Answers:

Use the strtotime method provided by PHP.

date('Y-m-d', strtotime('-7 days'))

Thanks to @lonesomeday for pointing out my mistake in the comments 😉

Answer:

With this, as with all PHP date stuff, it’s nicer to use the DateTime class.

$date = new DateTime('7 days ago');
echo $date->format('Y-m-d');