Page 1 of 1

converting some numbers to make them mysql date happy?

Posted: Wed Apr 12, 2006 7:10 am
by robster
Hi there, I have some variables, and an example of their content may be this:

Code: Select all

$day = "1";
$month = "9";
$year = "2006"

I need to get these numbers into the mysql date format, which in this case would be "2006-09-01".

Can anyone describe what functions or tools or methods might be available to me to do this? I've had a programming break and am a bit rusty in the logic department :(

Any help GREATLY appreciated.


Rob

Posted: Wed Apr 12, 2006 7:38 am
by ntbd
er $db_friendly = "$year/$month/$day"; ?

too simple? ;)

Posted: Wed Apr 12, 2006 7:44 am
by robster
:) thanks but not quite...
Note the preceding 0's on the required data.

ie: 1 vs 01 and 6 vs 06.


Any suggestions?


Rob

Posted: Wed Apr 12, 2006 7:49 am
by ntbd
Sorry, this might be more useful.

Code: Select all

$date = "10-3-1987";
$date = preg_replace("/([0-9]+)-([0-9]+)-([0-9]+)/ie", "'\\3-'.str_pad('\\2', 2, '0', STR_PAD_LEFT).'-'.str_pad('\\1', 2, '0', STR_PAD_LEFT)", $date);
Then insert $date. It wont automaticaly pad the year though.

Posted: Wed Apr 12, 2006 7:50 am
by Weirdan

Code: Select all

$date = str_pad($year, 4, '0', STR_PAD_LEFT) . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);

Posted: Wed Apr 12, 2006 7:52 am
by cj5
Go to the manual and read about sprintf()

Posted: Wed Apr 12, 2006 7:59 am
by robster
Thank you all so much... wow, bit to re-learn :)


Rob