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.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie