SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date Formatting Issues

    I have a table with a list of jobs, on the page (http://jobs.localvenues.net/) to output them I am showing all the information and the closing date using this code...

    PHP Code:
    <?php echo date("d F Y"$row['applyend']); ?>
    Where applyend is the field name in the database with this value...

    20051116120000

    However when it is outputted it returns this...

    18 January 2038

    Where am I going wrong? Any help appriciated.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date formatting

    The output is formatted the way you wrote it in the date() line. You didn't show what output format you wanted, but there is a list of possible choices, and a dozen or more examples here :
    http://us3.php.net/manual/en/function.date.php

  3. #3
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I did... d F Y

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date formatting

    And that's what it output. I quess I missed your question, what is the problem?

  5. #5
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DA Master
    I have a table with a list of jobs, on the page (http://jobs.localvenues.net/) to output them I am showing all the information and the closing date using this code...

    PHP Code:
    <?php echo date("d F Y"$row['applyend']); ?>
    Where applyend is the field name in the database with this value...

    20051116120000

    However when it is outputted it returns this...

    18 January 2038

    Where am I going wrong? Any help appriciated.
    That is my problem, it should output 16 November 2005

  6. #6
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    20051116120000 is not a valid UNIX timestamp. That's why it's showing the wrong date.
    Convert $row['applyend'] to a UNIX timestamp (using mktime()), and the date() will work.

  7. #7
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought the Timestamp type in MySQL was a Unix Timestamp?

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date formatting

    Sorry, I don't read timestamp-speak and I didn't notice the actual date, only it's format. On closer inspection however, it seems the date is an "out of range" problem ie
    from the php docs
    "Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). On windows this range is limited from 01-01-1970 to 19-01-2038."
    Are you sure the timestamp numbers are correct? Are you generating the timestamp with MySQL and reading it with PHP?

  9. #9
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague
    Are you generating the timestamp with MySQL and reading it with PHP?
    Yeah.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DA Master
    That is my problem, it should output 16 November 2005

    Why not just do it in your query...

    PHP Code:
    $sql "SELECT DATE_FORMAT( date_field_name, '%d %M %Y' ) AS converted_date FROM table_name WHERE ...";

    while ( 
    $row = ... mysql result ... )
    {
        echo 
    $row['converted_date'];

    printf

  11. #11
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice idea, printf, what column type does it need to be?

  12. #12
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any DATETIME, DATE, or TIMESTAMP.

    If any date is invalid it is converted (year = 0000, month = 00, day = 00 ) same goes for hour minute and seconds!

    So if year was invalid it would ouput '0000'! If you were using '%Y'

    That's why it a good idea to validate your data before inserting it because MySQL only does simple validation on any date type

    Quote Manual

    The MySQL server performs only basic checking on the validity of a date: The ranges for year, month, and day are 1000 to 9999, 00 to 12, and 00 to 31, respectively. Any date containing parts not within these ranges is subject to conversion to '0000-00-00'. Please note that this still allows you to store invalid dates such as '2002-04-31'. To ensure that a date is valid, perform a check in your application.
    printf

  13. #13
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by printf
    Any DATETIME, DATE, or TIMESTAMP.

    If any date is invalid it is converted (year = 0000, month = 00, day = 00 ) same goes for hour minute and seconds!

    So if year was invalid it would ouput '0000'! If you were using '%Y'

    printf
    Great stuff printf, thanks, and welcome to the forums...

    http://jobs.localvenues.net/

  14. #14
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DA Master
    Great stuff printf, thanks, and welcome to the forums...

    http://jobs.localvenues.net/

    Thanks for the welcome!

    printf

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    timestamp

    Going back to the top, if you still want to use your php code as shown, use UNIX_TIMESTAMP in your MySQL.

  16. #16
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help mittineaque, much apprciated.

  17. #17
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Along with using mtkime you could also do
    PHP Code:
    date("d F Y"strtotime($row['applyend'])) 
    I love strtotime, greatest fuction ever.
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM

  18. #18
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by -Oz-
    Along with using mtkime you could also do
    PHP Code:
    date("d F Y"strtotime($row['applyend'])) 
    I love strtotime, greatest fuction ever.
    Really? I will give all these a try, thanks for your time all.


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
  •