Saturday, 10 January 2015

PHP convert date yyyy-mm-dd to dd-mm-yyyy

Dates that come from MySql databases are a bit back to front for people to read.  The year first followed by months and then days.  In Australia, New Zealand, Europe and many other countries it is dd-mm-yyyy so this is a small function to turn this around to be more readable.
On your page where you call the date from the datbase, put the date into a variable then call the function.


3
4
5
//This is the function
function changeDate($backwardsDate){
 $newDate = date("d-m-Y", strtotime($backwardsDate));
 return $newDate;
}

//Put the date into a variable
$backwardsDate = 2012-10-28;
//Call the function
echo changeDate($backwardsDate);
// Once processed by the function it will output 
// 28-10-2012

No comments:

Post a Comment