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:

PHP:
  1. //PHP5 E_STRICT COMPLIANCE
  2. 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:

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

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

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

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | PHP | PHP Calculate Dates