PHP and mySQL dates
Normally this is not an issue but a recent project I was working on meant that the normal way of using dates has caused an issue. When converting a date that is formatted dd/mm/yyyy to yyyy-mm-dd hh:mm:ss didn’t work the way I wanted it to. A way round this is to format the UK date in to the US date format then in to the mySQL datetime format and of course vice-versa on retrieving the date from the database.
I created 2 functions, php_to_mysql() and mysql_to_php
PHP to mySQL
//Break the date up in to an array
$php = explode("/", $php);
//Re=arrange the date in to the US format
$php = $php . "/" . $php . "/" . $php;
//Convert the date (string) in to php time format
$php = strtotime($php);
//Format the time value in to a php variable in the format of mySQL datetime field
$php = date('Y-m-d H:i:s', $php);
mySQL to PHP
//Change the mySQL date to a time value.
$mysql = strtotime($mysql);
//Format the time value to the UK date format
$mysql = date("d/m/Y", $mysql);
This one is much simplier. As we do not need to involve a re-arrangment of the date from US to UK, we can just format the time value to the UK format straight off.