Results 1 to 6 of 6

Thread: PHP Date

  1. #1
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Question PHP Date

    hey guys

    im kinda new to php and need some help with date format conversions. specifically when selected from a SQL database. Basically, i have a date column in my datatable and i call it in with
    PHP Code:
    $row['Date'
    it will then show up like it is in the table as 2011-09-24. is there any way to convert this to something like September 9, 2011???????

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <?php
    function convert_time($x) {
      $spl = explode("-", $x);
      $y = $spl[0]; $m = $spl[1]; $d = $spl[2];
      $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
      return $months[$m-1]." ".$d.", ".$y;
    }
    //example usage
    echo convert_time("2011-04-20");
    //or more specifically, convert_time($row['Date']);
    ?>
    Note: this is untested, however it should work.
    - Mike

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Here's a far simpler way.
    PHP Code:
    date("F j, Y"strtotime($row['Date'])) 

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

    Default

    Quote Originally Posted by fastsol1 View Post
    Here's a far simpler way.
    PHP Code:
    date("F j, Y"strtotime($row['Date'])) 
    absolutely. to expand on that, I highly recommend NOT using mysql's version of 'DATETIME' or 'TIMESTAMP'. It saves human-readable date strings, but that makes it difficult to do anything with them (compare dates, change formats, etc.).

    a better solution is to save a unix timestamp (e.g., using time() ) which you can operate on or convert into a readable string using date() as fastsol does above.

  5. #5
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks everybody!!!

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Quote Originally Posted by traq View Post
    absolutely. to expand on that, I highly recommend NOT using mysql's version of 'DATETIME' or 'TIMESTAMP'. It saves human-readable date strings, but that makes it difficult to do anything with them (compare dates, change formats, etc.).

    a better solution is to save a unix timestamp (e.g., using time() ) which you can operate on or convert into a readable string using date() as fastsol does above.
    I second this. ALWAYS use time() for storing time. It's easy to sort, compare, store and convert. And it stores time down to the second.
    - Josh

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
  •