PHP Calculate Dates

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')));

  • Digg
  • TwitThis
  • del.icio.us
  • Netvouz
  • description
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati
  • Facebook
  • Google
  • LinkedIn
  • Live
  • MySpace
  • Ping.fm
  • Yahoo! Buzz
  • E-mail this story to a friend!



Home | PHP | PHP Calculate Dates