Get previous and next date for a specific date in PHP

0
178
PHP Programming Language
PHP Programming Language

This is a simple tip to get previous and next date for a specific date in PHP language.

In case you want to get dates for today,


$previous_date = date('Y-m-d',strtotime("yesterday"));
$next_date     = date('Y-m-d',strtotime("tomorrow"));

or you can use this


$previous_date = date('Y-m-d',strtotime("-1 day"));
$next_date     = date('Y-m-d',strtotime("+1 day"));

If you want to get previous and next date for a specific date in the past or future, then you need to provide that date in date-time query.


$date = '2017-07-12';

$previous_date = date('Y-m-d', strtotime($date . ' -1 day');
$next_date     = date('Y-m-d', strtotime($date . ' +1 day');

You can further apply for week, month, year or any valid date-time words.

Please refer to strtotime for more information.