We help IT Professionals succeed at work.

Format Date

tonelm54
tonelm54 asked
on
119 Views
Last Modified: 2017-05-17
Ive got a date which is being pulled in from a database as "18/04/2016 17:45:53" and Im trying to convert it to a date that I can then format correctly, so Ive got the following:-
    $strDate = "18/04/2016 17:45:53";
    $newformat = date('d/m/Y H:i:s', $strDate);

    echo date("Y/m/d H:i:s", $newformat);

Open in new window


But all Im getting is:-

E_NOTICE : type 8 -- A non well formed numeric value encountered -- at line 3
E_NOTICE : type 8 -- A non well formed numeric value encountered -- at line 5

But it does echo out as:-
1969/12/31 19:00:31

So, I dont understand what the problem is. Any ideas?
Comment
Watch Question

Author

Commented:
Sorry, Ive also tried strtotime:-
    $strDate = "18/04/2016 17:45:53";
    $newformat = strtotime($strDate);

    echo date("Y/m/d H:i:s", $newformat);

Open in new window


Which returns:-
1969/12/31 19:00:00

which is obviously wrong :-(
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Now having said that, please read the articles (really!) because the meaning of slashes and dashes in date/time strings are very different and can change the output of the functions.  They imply the specific date format in the procedural approach, and in the default methods of the object-oriented approach.

The object-oriented way has some more control over the input date formats.
http://php.net/manual/en/datetime.createfromformat.php
Dorababu MSenior Software Engineer
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
What was the expected format ?
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Ive got a date which is being pulled in from a database as "18/04/2016 17:45:53"
Yikes! - I just noticed the part about this date being in the database.  Something is wrong in the design if you're getting that from a database query.  Here are the general guidelines...

1. Define your columns DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'
2. For all internal representations of DATETIME values use the ISO-8601 format
3. When you insert values into DATETIME columns use the ISO-8601 format
4. For external representations in procedural code, change the ISO-8601 format to your preferred formatting by using strtotime() and date() functions
5. When you are working working with object-oriented code, format the DateTime object with the format() method

Here is what I might do to move forward on this issue.

1. Use ALTER TABLE to add a new DATETIME column to the table, according to #1 above, then...
2. SELECT the id and the existing incorrectly formatted datetime values
3. Iterate over the results set, making ISO-8601 datetime strings and updating each of the rows with the correctly formatted datetime values

Once you're sure that you do not have any dependencies on the incorrect format columns, change the PHP scripts to use the new columns.  This means changing the scripts that insert the values into these columns and changing the scripts that use these values.  Eventually, you will be able to use ALTER TABLE to drop the old columns.

Author

Commented:
Didnt add in the comment on close, I changed the query to export the date in a more readable context for PHP then used the suggested solution to convert into a date which I could then process.

Thank you
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points -- it's a great question, and once you get used to using the ISO-8601 formats for your internal representations, you will never want to use anything else!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.