insert date using PDO query

Hi,

I am trying to insert the date below into the driver licence numbers table. Currently it is not working as MySQL expects the Y-m-d format. When using the old mysql_query function to define the date format I used to use the STR_TO_DATE function but unsure how to do it here.

I have tested the query and it works i.e. it inserts the licence number just not the date.

[php]$posted_driverLicenceNumber = ‘78696565887’;
$dateAdded= ‘13/02/1992’;

$insert_new_driverLicenceNumber = “INSERT INTO DriverLicenceNumbers(DLN_Number, DLN_DateAdded)
VALUES (:posted_driverLicenceNumber, :dateAdded)”;
$q = $db->prepare($insert_new_driverLicenceNumber);
$q->execute(array(’:posted_driverLicenceNumber’=>$posted_driverLicenceNumber,
‘:dateAdded’=>$dateAdded));[/php]

Many Thanks

[php]$date = ‘13/02/1992’;

echo $date = date(“Y-m-d H:i:s”,strtotime(str_replace(’/’,’-’,$date)));[/php]

First Kevin’s way is more elegant, but you could also do it this way:

[php]$date = ‘13/02/1992’;
$dateFormated = explode(’/’, $date);
echo $date = $dateFormated[2] . ‘-’ . $dateFormated[1] . ‘-’ . $dateFormated[0];[/php]

The only reason I shown this is to demonstrate to others there are more than one way in programming to resolve issues. I’m sure if someone played around even more there will probably be other ways to come up with the same result.

Sponsor our Newsletter | Privacy Policy | Terms of Service