Date work is always icky, but luckily PHP has some pretty nifty functions that can pretty much do all the heavy lifting for you.

One of most commonly faced problems is how to add either a number of days, months or years to a given date, and thanks to PHP’s fantastic strtotime function and its natural language (well sort of) parsing, this becomes pretty trivial to solve.

Taken from the PHP manual: The strtotime function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

To see it in action against the current date, try running:

echo '<p>' . date('Y-m-d', strtotime('+1 day')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+2 weeks')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+3 months')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+4 years')) . '</p>';

Of course, if you want to use a different base for its calculations, you would use the function like so:

echo '<p>' . date('Y-m-d', strtotime('+1 day', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+2 weeks', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+3 months', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+4 years', strtotime('2000-12-31'))) . '</p>';


Related Link: