PHP Calculate Dates

Like this blog? Consider exploring one of our sponsored banner ads...

With the PHP date function it is very easy to format dates however you like. If you are running PHP5 with E_STRICT error_reporting, be sure to set your timezone before using the PHP date function:

//PHP5 E_STRICT COMPLIANCE
date_default_timezone_set('America/Los_Angeles');

Here is the classic super simple example of how to format a date for a MySQL datetime field:

 $date = date('Y-m-d H:i:s');

This is how I calculate dates/times in the future or past:

//NEXT MONTH :: MONTH, YEAR
$date = date('F, Y',mktime(date('H'), date('i'), date('s'), date('m')+1 , date('d'), date('Y'))); 
 
//YESTERDAY :: MONTH DAY, YEAR
$date = date('F m, Y',mktime(date('H'), date('i'), date('s'), date('m') , date('d')-1, date('Y')));

About this entry