SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Simple format date from MySQL database

  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple format date from MySQL database

    Hi,

    I've got a date being pulled from a MySQL date field. In the database the date look like this:

    2007-05-10

    I want to format this so it appears:

    10th May 07


    I've tried this:

    Code:
    print date("j M y",$db_field['tstamp'])
    But that renders in the browser as:

    1 Jan 70



    It should say 10 May 2007.

    Can anyone see why this is happening?

    Thanks folks...

  2. #2
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the following query

    select *, date_format(date_field_name,'%D %b %y') as MyDate from table_name;
    Barbara

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by danwednesday View Post
    Hi,

    I've got a date being pulled from a MySQL date field. In the database the date look like this:

    2007-05-10

    I want to format this so it appears:

    10th May 07


    I've tried this:

    Code:
    print date("j M y",$db_field['tstamp'])
    But that renders in the browser as:

    1 Jan 70



    It should say 10 May 2007.

    Can anyone see why this is happening?

    Thanks folks...
    What do you get when you just print that field out without formatting?

  4. #4
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hartmann View Post
    What do you get when you just print that field out without formatting?
    Just 2007-05-10

  5. #5
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by danwednesday View Post
    Just 2007-05-10
    Just realized something obvious, that is a date string you are passing, while the date function takes in a true timestamp, which is why you are getting the weird date.

    Try this:

    PHP Code:
    print date("j M y"strtotime($db_field['tstamp'])) 

  6. #6
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks very much Hartmann!

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
  •