Two PHP functions to handle MySQL dates

MySQL dates (datetime type) usually follow this pattern: 2010-03-11 17:18:02. Here are two PHP functions to create and format MySQL dates on your site/blog:

function create_sql_date() 
  // MySQL datetime: YYYY-MM-DD hh:mm:ss
  $timestamp = time();
  return strftime('%Y-%m-%d %H:%M:%S', $timestamp); 
function format_sql_date($date) 
  $date_time = new DateTime($date);
  return ($date_time->format('F j, Y'));   

The first function is useful to create fresh dates to be inserted in the database, while the latter can be deployed to format MySQL dates after retrieving them from a query.

Leave a Reply

Note: Only a member of this blog may post a comment.