This tutorial will show you how can you calculate either future date from a particular date to “plus a number of given days” or past date from a particular date to “minus a number of given days”.
Future or past date calculation may be required for various purposes such as to calculate the age of a person on future date or on past date.
I will show you this calculation in PHP language. If you need to calculate in Java please read here https://www.roytuts.com/future-or-past-date-in-java/
We first need to setup an appropriate timezone otherwise php sometimes give warnings. I have set “Asia/Kolkata”, you can set according to your timezone.
date_default_timezone_set('Asia/Kolkata'); $date_format = 'Y-m-d'; //date format $date = "2015-02-25"; //date from which the next or past date will be calculated $future_date = date($date_format, strtotime($date. ' + 280 days')); //future date after adding 280 days to the above date echo 'Future Date : '.$future_date.'('.$date_format.')'; echo "\r\n"; $past_date = date($date_format, strtotime($date. ' - 30 days')); //past date after subtracting 30 days from the above date echo 'Past date : '.$past_date.'('.$date_format.')';
Future Date : 2015-12-02(Y-m-d) Past date : 2015-01-26(Y-m-d)
That’s all. Thanks for your reading.