jphillip724
jphillip724

Reputation: 269

php date to mysql returns 0's

Hello i am trying to add a date time from PHP into my MySQL database, in the database I have tried date time and time stamp and I have tried it with and without current_time set as the default option, in my php I have the following.

date_default_timezone_set("Europe/London");
$date = date('m/d/Y h:i:s a');

My hope was to just add the $date into the value's part of the upload query however in all cases it comes back as 0's, it echo's the date fine, however it uplaod's 0's, any help is appreciated, Thanks.

Upvotes: 2

Views: 1483

Answers (1)

John Conde
John Conde

Reputation: 219844

'm/d/Y h:i:s a is not a standard MySQL datetime format so unless you are storing it as varchar/char you will get the results you are seeing.

Your options are to:

  1. Store the date in a standard format (datetime Y-m-d H:i:s, timestamp) and convert it to whatever format you want during the query (recommended)

  2. Store it as a string but lose all of the datetime functionality MySQL offers. Losing this functionality will make working with dates in your queries and PHP very painful and is not recommended to do.

Upvotes: 5

Related Questions