Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: How to get date_format() to work properly

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Are you pulling the values from your DB?

    MySQL defaults to 0000-00-00 when it can't understand a given date. Likewise, strtotime() defaults to 1969-12-31 when it doesn't understand a date. (I don't know why you're getting "-0001-11-30"; I couldn't reproduce that.)
    PHP Code:
    <?php

    //  are you _sure_ that   $client['edited'] === '0000-00-00'  ...?
    //  try passing '0000-00-00' directly ...?

    echo date"Y-m-d",strtotime'0000-00-00' ) );
    // output: 1969-12-31

    echo date"Y-m-d",strtotime'2012-05-30' ) );
    // output: 2012-05-30

  2. #12
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Yes, I am pulling the values from a mySQL database. When the record has never been edited, the `edited` field is NULL. It displays as 0000-00-00 00:00:00. I am sure about that. When I use echo $client['edited'] with no formatting, that is what I get.
    Last edited by kuau; 05-31-2012 at 03:40 PM. Reason: added code tags

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    odd - I still get 1969-12-31, even with the 00:00:00 part. That is the correct behavior, according to php.net. (MySQL giving "0000-00-00 00:00:00" for NULL is also the correct behavior.)

    You said you did a conditional (if) check on the $client['edited']; what did that look like?
    Something like this should be sufficient...
    PHP Code:
    <?php

    if( $client['edited'] == '0000-00-00 00:00:00' ){
        print 
    "this entry has never been edited.";
        
    // or set it to today's date, or whatever
    }

  4. The Following User Says Thank You to traq For This Useful Post:

    kuau (06-01-2012)

  5. #14
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear traq: I'll try that again, but right now I thought I would try one of your earlier suggestions about doing the formatting in the SQL command. BTW, is it true that if you wish to specify one field that you have to name them all? I went from SELECT * to a huge list of columns.

    Anyway, what I did is displaying as blank. I must be doing something else wrong now... oh, I figured it out... here is what works...

    Code:
    SELECT `client_id`, `agent`, `lastname`, `firstname`, ...etc... , date_format(`edited`,'%Y-%m-%d') AS `edited` FROM client
    Then in the report: echo $client['edited']

    It works! Yahoo! Thanks for all your help.
    Last edited by kuau; 06-01-2012 at 12:29 AM. Reason: figured it out

  6. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by kuau View Post
    BTW, is it true that if you wish to specify one field that you have to name them all? I went from SELECT * to a huge list of columns. ...
    I'm glad to hear you got it sorted out.

    If I understand your question, no, you don't *have* to list all the columns - you can do like:
    Code:
    SELECT *,date_format(`edited`,'%Y-%m-%d') AS `edited_formatted`FROM client ...
    While it's easier, I don't think it's "better," per se. But it does work (note that you can't use one of the actual column names if you do this -as `edited`won't work; as `edited_something` will- mysql will complain if there's a column name conflict).

  7. The Following User Says Thank You to traq For This Useful Post:

    kuau (06-01-2012)

  8. #16
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear traq: Yes, that is exactly what I wanted to know (SELECT *,date_format...). Thank you.

    The funny thing is that mysql didn't complain when I used an actual column name after AS. I did it successfully yesterday too when I had to re-run a script on a different column name so it saved me having to edit the second script which was great. I hope it is OK to keep doing it (?).

    My report looks so good now!

  9. #17
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    if you use *, it will become a problem: e.g.,
    Code:
    # this works just fine, as long as you don't also SELECT `example`
    SELECT `one`,`two`,`three`,DATE_FORMAT( `example`,'%Y-%m-%d' ) as `example` ...
    
    # however, this will cause an error:
    SELECT *,DATE_FORMAT( `example`,'%Y-%m-%d' ) as `example` ...
    # because you're trying to use `example` as an alias, 
    #   when you've already selected a column with that name (via *).
    # MySQL doesn't like the ambiguity.
    
    SELECT *,DATE_FORMAT( `example`,'%Y-%m-%d' ) as `no_col_has_this_name` ...
    # working fine again.

  10. #18
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear traq: I just got back home so I didn't see this until now. I had already changed it this afternoon to...

    Code:
    $sql = "SELECT *, date_format(`edited`,'%Y-%m-%d') AS `edited` FROM client
    That is my exact code. I swear to god it worked fine right off the bat and I just checked again and it is working perfectly. Maybe my version of mysql is more forgiving than yours. Thanks for solving my problem for me. Now if I can just get the array one solved, I'll be happy.

  11. #19
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by kuau View Post
    it worked fine right off the bat and I just checked again and it is working perfectly. ...
    How very strange... I've actually tried this, and I get an error. I'm glad it's working, though!

  12. #20
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Yeah, isn't it ironic that what the manual says will work doesn't work, but what shouldn't work, does. Enough to make you crazy...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •