2

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.

2
  • 1
    Your date format is not the correct format for MySQL - You need to save it as Y-m-d H:i:s Commented Nov 14, 2013 at 14:40
  • 1
    It should be Y/m/d H:i:s Commented Nov 14, 2013 at 14:42

1 Answer 1

5

'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.

Sign up to request clarification or add additional context in comments.

4 Comments

Another option is to use strtotime() and than convert the date via php
Thanks i got it working, dont know where the A came from :S, once i swapped the m/d/y to Y/m/d it worked, Thanks for help will mark as answer when i can :)
@jphillip724 This is VERY IMPORTANT - Make sure you have an uppercase H as well - lower case will result in 12 hour time and half of your times could be 12 hours out. There is a difference between H:i:s and h:i:s!
I know this is an old thread, and I can understand the comment about maintaining dates in mySQL but I can vouch that trying to get the formats into mySQL can be sometimes be very problematic. I have found that manipulating outside mySQL in say PHP can give you a bit more flexibility as you know that you are always dealing with text when going back to mySQL. Anyway my 2c

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.