PHP Date conversion Functions

It was pointed out to me the other day that I don’t have much in the way of technical stuff in my blog. This, I have to admit, has been an oversight of mine as I did intend to have technical stuff regularly in my blog.


So here goes with something useful for PHP


/**
* This function takes in a date in dd/mm/yyyy format and converts it to yyyy-mm-dd
* for storing on mysql database
*
* @param string $thedate = date in dd/mm/yyyy format
*/

function convertDisplayDate($thedate)
{
$year = substr($thedate, 6, 4);
$month = substr($thedate, 3, 2);
$day = substr($thedate, 0, 2);
$date = $year;
$date .= “-“;
$date .= $month;
$date .= “-“;
$date .= $day;
return $date;
}


/**
* This function takes in a date in yyyy-mm-dd format and converts it to dd/mm/yyyy
* for displaying on UK front-end web pages
*
* @param string $thedate = date in yyyy-mm-dd format
*/

function convertMySQLDate($thedate)
{
$year = substr($thedate, 0, 4);
$month = substr($thedate, 5, 2);
$day = substr($thedate, 8, 2);
$date = $day;
$date .= “/”;
$date .= $month;
$date .= “/”;
$date .= $year;
return $date;
}

This entry was posted in Code, Php and tagged , , , , . Bookmark the permalink.

Leave a Reply