Rewriting MySQL Date to PHP Date Format

I have been always searching for a PHP function that could rewrite the mysql date to a descriptive PHP date form. Dates stored in MySQL database usually in the format of YYYY-MM-DD.

There will be situations where you might want to output the date stored in mysql in a more descriptive for users in your website. For example you might want to show 2007-03-11 as 11 March 2007 which is more easily readable and easy to understand.

I wrote this MySQL2Date() PHP function to rewrite the date.

/* Mysql2Date() Function
Converts/rewrites date in MySQL format yyyy-mm-dd to
PHP date. This can come handy when you want to print
the expanded ate like 20 Jan 2005 instead of outputting
flat numbered mysql date.

Desired Format: yyyy-mm-dd
Example: 2006-05-11 will print as 11 May 2006
*/

function Mysql2Date($mysql_date)
{
list($year,$month,$day) = explode('-',$mysql_date);
$time_stamp = mktime(0,0,0,$month,$day,$year);
/*
PHP date format
Y = 2007
M = Jan to Dec
d = 2 digit date with padded zeroes
You can change to any date format. see PHP date manual
*/

$date_format = date("d M Y",$time_stamp);
return $date_format;
}

Similar Posts:

Tags:

Balakrishnan Prabhu

Mr. Balakrishnan Prabhu is the founder of Corpocrat magazine. He is also the founder of Best Citizenships (BC), assisting wealthy individuals with with global citizenship and residency programs in Europe. His other interests are Linux, Machine learning, Wordpress, etc. You can contact him here

  • OperatorNo9

    function Mysql2Date($mysql_date)
    {
    return date(“d M Y”, strtotime($mysql_date) );
    }

  • Here is a useful website for formatting dates using the mysql date_format function