Add Time to the Date Function in PHP
by Bandicoot Marketing on | Posted in Tutorials | 9 comments
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.
9 comments for “Add Time to the Date Function in PHP”