get X days back date from current date

How to get 7(X) days back date from current date?

PHP is very strong server side programming language which simplifies lots of small small tasks like converting date formats etc.

I was working on some task, where I was suppose to get 7 days back date from current date. Actually the task is very simple, I am sharing the code here. Have a look at it and let me know if any one can find better way than this.


$format = 'YmdhIS'; // YOU CAN USE ANY FORMAT WHICH YOU WANT

$date = date ( $format );

// - 7 days from today
$prevDate = date ( $format, strtotime ( '-7 day' . $date ) );

Note: If you want to get yesterdays date then simply do -1 instead of -7.

How to get 7(X) months back month from current month?

// - 7 months from today
$prevDate = date ( $format, strtotime ( '-7 month' . $date ) );

Its so simple.

0 comments:

Post a Comment

Share this post with your friends