Apr
22
2009
Add Time to the Date Function in PHP
I just needed to do this for a client’s site to set an expiration date for a user and I thought it might be something that other programmers might like to know how to do. If you need to take today’s date and add time to it it is pretty simple. There is a PHP function called date() and another called strtotime(). You need to use both to make this happen.
$date = date('Y-m-d'); $timestamp = strtotime(date("Y-m-d", strtotime($date)) . " + 1 month"); $expired = date('Y-m-d', $timestamp); |
If you echoed out $date you would get today’s date formatted like “2009-04-21″.
If you echoed out $expired you would get today’s date plus 1 month, formatted like “2009-05-21″.
You can replace the “1 month” with “14 days” or “2 years” or whichever increment you need.



This just saved me a huge headache. Let me do something with Joomla that was driving me insane.
Just wanted to say thanks!
That’s a little more complicated than it needs to be.
thanks
thanks ryan…
this is very helpful for me