Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: How to get date_format() to work properly

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

    Default How to get date_format() to work properly

    I am trying to do a very simple thing but I am getting erratic results. I believe I am doing it exactly as defined in the php manual, but it is not working. In the php manual a user, James Meyer, commented that php 5.2 and 5.3+ have issues with this command. So what works?

    All I want to do is leave the time off a datetime value when displayed on a webpage. I have tried every possible way I can imagine using both date() and date_format(), but I either get all the dates as 1969-12-31 or nothing at all. I use an SQL command to retrieve the values from the database and then this php to display them...

    Code:
    <?php echo date_format($client['edited'], 'Y-m-d');?>
    If I just use <?php echo $client['edited'];?> the date displays the correct date but with the time also, which wraps and messes up the display.

    I just noticed this message in the error log... in the SQL command I use $client = mysql_fetch_assoc($result) and do not convert the value from the DateTime field...

    Code:
    PHP Warning:  date_format() expects parameter 1 to be DateTime, string given in /public_html/php/file-name.php on line 20
    Last edited by kuau; 05-25-2012 at 05:37 PM. Reason: added info

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    What is the value of $client['edited'] (could you give an example)
    I thought to be able to use date ( and I assume date_format ) too, the parameter has to be a time stamp ( time() ).

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

    Default

    The field `edited` is a Timestamp datatype, for example 2012-01-06 18:26:16. It registers the last time the record was edited.

    I just want to display 2012-01-06 without the 18:26:16. Simple you would think.

  4. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Try

    PHP Code:
    echo date("Y-m-d"$client['edited']); 
    If $client['edited'] is a timestamp (eg. 1337943587) it should work...

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

    Default

    OK, I tried that and nothing displayed but I got this error message...

    Code:
    PHP Warning:  date_format() expects parameter 1 to be DateTime, string given in /public_html/php/client-listing.php on line 20
    It's very strange because the table column is defined "edited timestamp" yet it displays in phpMyAdmin as 2012-01-06 18:26:16 and won't take commands.

    So I put it back to <?php echo $client['edited'];?> and now it displays the proper date and wraps again. Do you think it is a bug in this version of php?

  6. #6
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Are you using:

    PHP Code:
    echo date("Y-m-d"$client['edited']); 
    or


    PHP Code:
    echo date_format("Y-m-d"$client['edited']); 
    The former is correct.

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

    Default

    I tried both. Neither worked.

  8. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    date() and date_time() both need datetime objects as their parameters (eg. 1337943587) while you're using sql timestamps - (eg. 2012-01-06 18:26:16). That's why it is producing that error when you try and use date/date_format.

    I don't use sql timestamps (I find them annoying) (I use unix time instead ( time() ) so I'm not sure how to answer this one.

    The only thing I can suggest, is google convert sql timestamps to unix timestamps, then running the code you already had...

    I haven't really looked at it, but this might be what you're looking for.

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

    Default

    Actually, the mysql DATETIME format -while needing some "getting used to"- is preferred, since it is more flexible - it's more easily translated to PHP's internal date/time format (64bit int). It also allows a greater date range than unix timestamps (about 292 billion years past/future, compared to the unix timestamps, which will run out of numbers in 2038).

    It also allows you to do manipulations on the date via MySQL (i.e., as part of the query instead of afterwards in PHP, reducing overhead).

    But, for your purposes, either will work.

    PHP Code:
    <?php
    // functional approach:
    print date"Y-m-d",strtotime$client['edited'] ) );

    // object-oriented approach:
    $date = new DateTime$client['edited'] );
    print 
    $date->format"Y-m-d" );

    // (both result in the same output.)

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

    Default

    Dear traq:

    I tried what you suggested. In fact, that was the very first thing I tried thinking it would work, but wherever the record had not yet been edited, ie. the date is 0000-00-00, it would display the date as -0001-11-30. I even tried adding an if($edited == '0000-00-00'){ etc. which didn't help.

    I know this should be a simple thing but nothing seems to work as expected. Here is my code which displays 0000-00-00 as -0001-11-30...

    Code:
    <?php echo date( "Y-m-d",strtotime($client['edited']));?></div>
    Have you tried it yourself?

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
  •