Page 1 of 1

[PHP - MySQL] Date conversion code

Posted: Thu Jun 25, 2009 10:34 am
by cheeney
Hi

I'm hoping for a little help here.

I'm writing a very simple database app (using MySQL). I need the PHP code to be able to take a date like 25/6/09 and convert it into whatever format is needed by MySQL.

This is where I need the help. I've looked around a bit and have found stuff that will handle 25/06/2009 but I'd like the code to be more forgiving. Unfortunately, I've found no suitable examples. :(


Any help/links/code is greatly appreciated.


Thanks in advance

Re: [PHP - MySQL] Date conversion code

Posted: Thu Jun 25, 2009 2:34 pm
by pickle
I think you'll need to explode it, rebuild it into a timestamp, then use the MySQL function FROM_UNIXTIME() to convert it to a MySQL date stamp:

Code: Select all

$raw = '26/5/09';
list($day,$month,$year) = explode('/',$raw);
$stamp = mktime(0,0,0,$month,$day,'20'."$year");//treat $year as a string to prevent the leading zero from getting stripped
$query = 'INSERT INTO `myTable` (`dateField`) VALUES (FROM_UNIXTIME('.$stamp.'))';