I am trying to work out how to convert UK date format (DD/MM/YYY) to a format that will be accepted by MySQL (YYYY-MM-DD), so far I have been almost successful by using the following:-
[tt]
$mysqlDate = "1/1/2005";
//split day, month and year into an array
$date_array = explode("/", $$mysqlDate );
// trim off leading zeros from month and day
$date_array[0] = ltrim($date_array[0],'0');
$date_array[1] = ltrim($date_array[1],'0');
//repad day and month with 0, if applicable
$$mysqlDate = $date_array[2] . "-" . ($date_array[1] < 10 ? "0" : "" .$date_array[1] . "-" . ($date_array[1] < 10 ? "0" : "" . $date_array[0];
echo $$mysqlDate ;
[/tt]
Which appears to work, until I try a date of 10/1/2005 or 10/01/2005, which for some reason give output of 2005-01-010.
Can anyone spot what the problem may be please?
Is there a better way to do this?
thanks
[tt]
$mysqlDate = "1/1/2005";
//split day, month and year into an array
$date_array = explode("/", $$mysqlDate );
// trim off leading zeros from month and day
$date_array[0] = ltrim($date_array[0],'0');
$date_array[1] = ltrim($date_array[1],'0');
//repad day and month with 0, if applicable
$$mysqlDate = $date_array[2] . "-" . ($date_array[1] < 10 ? "0" : "" .$date_array[1] . "-" . ($date_array[1] < 10 ? "0" : "" . $date_array[0];
echo $$mysqlDate ;
[/tt]
Which appears to work, until I try a date of 10/1/2005 or 10/01/2005, which for some reason give output of 2005-01-010.
Can anyone spot what the problem may be please?
Is there a better way to do this?
thanks