MySQL Date to UK Date and back to MySQL

Soldato
Joined
6 Dec 2002
Posts
3,400
Location
North East
Hello,

I've searched everywhere trying to find a solution to this problem but nowhere seems to cover it :(

I'm trying to convert a date from a database from YYYY-MM-DD to D/M/Y so that is readable on the page, then transfer it back to to YYYY-MM-DD so it can be put back into the database. I've tried:

$d = date("d-m-Y", strtotime($d));

Which works perfectly and produces the desired viewable format, however the problem arises when converting it back to put back into the database :(

$d = date("Y-m-d", strtotime($d));

Doesn't seem to work, any ideas?

Thanks in advance,

BeatMaster :D
 
strtotime() will probably be thinking its a US style date (MM/DD/YY) rather than UK style and will get confused.

Try using mktime() to build the unix timestamp to pass to the date function instead.
 
Works for me :confused:

Code:
$date = date('Y-m-d');
$pagedate = date('d-m-Y', strtotime($date));
$update = date('Y-m-d', strtotime($pagedate));

echo "$date $pagedate $update";

Prints "2007-01-26 26-01-2007 2007-01-26", as expected.
 
Back
Top Bottom