Monday, 6 August 2018

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;
}

0 comments:

Post a Comment