tags:

views:

91

answers:

4
$doba = explode("/", $dob);

$date = date("Y-m-d", mktime(0,0,0, $doba[0], $doba[1], $doba[2]));

The above code turns any date i pass through into 1999-11-30 and i know it was working yesterday. Date is correct when I echo $doba. Anyone have any ideas?

Cheers

+3  A: 

What is the format of $doba? Remember mktime's syntax goes hour, minute, second, month, day year which can be confusing.

Here's some examples:

$doba = explode('/', '1991/08/03');
echo(date('Y-m-d', mktime(0,0,0, $doba[1], $doba[2], $doba[0]);

$doba = explode('/', '03/08/1991');
echo(date('Y-m-d', mktime(0,0,0, $doba[1], $doba[0], $doba[2]);
Ross
+2  A: 

It is a bit overkill to use mktime in this case. Assuming $dob is in the following format:

MM/DD/YYYY

you could just to the following to acheive the same result (assuming $dob is always valid):

$doba = explode("/", $dob);
$date = vsprintf('%3$04d-%1$02d-%2$02d', $doba);
Andrew Moore
+3  A: 

or even easier: $date = date('Y-m-d', strtotime($dob))

jcoby
A: 

If you have issues with what jcoby said above, the strptime() command gives you more control by allowing you to specify the format as well.

R. Bemrose