Results 1 to 6 of 6

Thread: formatting datetime

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default formatting datetime

    After retrieving the date from the datetime field of my MySQL database I am trying to figure out how to format it into say the date("n/j/Y",$date); format. As far as I can tell the date() function formats the time(). Any ideas on how I can get the mysql datatype to work date()?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    use a variation of split or explode and mktime to convert the datetime field into a timestamp, then you can just pass that variable (the timestamp) to the date function.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    currently the datetime is in the format of 2008-02-26 00:00:00 and the timestamp is just a huge number of seconds as counted from 1970. How would you convert from one format to the other?

    (gonna get a haircut. I'll be back in a bit)

    I could use some substr_replace(), explode(), and substr(), math, and ceil() and/or floor() to change it to the right format, but I am trying to be conservative if I can If I can't that's fine too.
    Last edited by james438; 02-26-2008 at 06:53 PM. Reason: added last line.

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    there is the MySQL date_format() function. How can I retrieve the date using date_format() from row 3 of a table?

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    $date=str_replace(":"," ",$date);
    $date=str_replace("-"," ",$date);
    $date=explode(" ",$date);
    $date=mktime($date[3],$date[4],$date[5],$date[1],$date[2],$date[0]);
    $date=date("n/j/Y",$date);
    I was over thinking it. The above code is really easy and is much simpler than what I was trying to do earlier.

  6. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by james438 View Post
    there is the MySQL date_format() function. How can I retrieve the date using date_format() from row 3 of a table?
    Since you asked...

    PHP Code:
    $sql mysql_query("SELECT *, DATE_FORMAT(date, '%M %D, %Y') as date from table WHERE id='3'"); 

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
  •