Great PHP Date Reference

May 20th, 2008


Just thought we’d give some trackback attention to a much deserved reference page.

This reference gives one of the best explanations for addition of and subtraction using PHP’s built-in Date Function. Also, easily compute days in the future, past, etc using the simple natural language capabilities in the strtotime function:

PHP Dates, addition, future dates, using strotime

Special thanks to the guys/girls over at Art of Web

Additional PHP Date Functions

If you’re too lazy to even look at the link above, here are some of our useful PHP Date functions:

Convert MySQL Date to PHP Date Object

It is really almost redundant to create a function for it, but this will give you the idea. Also - this will return a date in php’s native unix format - Not human-readable format, you will have to convert it to the format you would like it after running this function.
function mysqlDateToPhpDate($mysqldate){
return strtotime($mysqldate);
}

Get Current Date (mysql format)

You use this function to save the current date to a mysql database.
date("Y-m-d H:i:s");

Convert to Mysql Format

You use this function to save date to a mysql database.
function toMysqlDate($somedate){
return date("Y-m-d H:i:s",$somedate);
}

Get Current Date (human-readable format)

date('m/d/y h:i a');

Get Next Year’s Date (in mysql format)

date((date("Y")+1)."-m-d H:i:s");

Add A Year to Date (not today)

Returns in Mysql Format (YYYY-mm-dd):

function addYearToMysql($somedate){
return date((date("Y")+1)."-m-d H:i:s",$somedate);
}

Is MySQL Date Passed

Pass a mysql date into this function to find out if it has passed.
function isDatePassed($somedate){
if(strtotime($somedate) > time())return false;
else return true;
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Leave a Reply

You must be logged in to post a comment.