SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date() function returning wrong date!

    Hi dudes,

    I am passing the date function a "datetime" value which is pulled from my DB. The date field looks like this:

    Code MySQL:
    date_created 	
    2010-04-16 04:45:32
    2010-04-16 04:58:18
    2010-04-16 06:02:17

    And the PHP like this:
    Code PHP:
    while($row = mysql_fetch_assoc($resourceID))
    {
    	echo date("D, M, Y", $row['date_created']);
    }

    And what I get output on screen is this:

    Wed, Dec, 1969

    As you can see there is no date in my table (shown above) with such an old date, any ideas what going on?

    many thanks

  2. #2
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second parameter to date requires a unix timestamp (an integer), whereas you are passing it the date coming from the database (a string). You need to read the string and convert it to a timestamp first.

    Luckily, a function exists to do that for you - strtotime.

    This should solve the issue:
    PHP Code:
    while($row mysql_fetch_assoc($resourceID))
    {
        echo 
    date("D, M, Y"strtotime($row['date_created']));

    Normally the code that you posted would output an error message explaining this to you; perhaps you have error reporting turned off on your server? If so, you might consider turning on error reporting for your development work (or writing your code on a development server with error reporting on before sending it to your production environment).

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tarh View Post
    The second parameter to date requires a unix timestamp (an integer), whereas you are passing it the date coming from the database (a string). You need to read the string and convert it to a timestamp first.

    Luckily, a function exists to do that for you - strtotime.

    This should solve the issue:
    PHP Code:
    while($row mysql_fetch_assoc($resourceID))
    {
        echo 
    date("D, M, Y"strtotime($row['date_created']));

    Normally the code that you posted would output an error message explaining this to you; perhaps you have error reporting turned off on your server? If so, you might consider turning on error reporting for your development work (or writing your code on a development server with error reporting on before sending it to your production environment).
    That is a really helpful post thanks a million. I get very lazy when im not programming in Java, I should have checked the PHP manual first !

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Tarh supplied the best answer, but depending on you needs your SQL select query could have turned the datetime to a unixtimestamp in your SQL statement, permitting you to skip that strtotime() step.

    unix_timestamp()

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Or adding to what Cups said, have MySQL format the date itself. Remove PHP out of the equation entirely.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •