SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Colorado, USA
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert TIME to 12-hr standard time

    Hi,

    I have a schedule that I am displaying on the web. Data is stored in MySQL. The schedule is sorted first by date, then by time, but I want to convert time from the 24-hour time clock to the 12-hour time clock without losing the order. Viewers are not interested in seeing 'military time', but I don't want a 1 pm event to show before an 8 am event. Any suggestions? Thanks!
    Snug Rooster

  2. #2
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Staffordshire, UK & Florida, USA
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can reformat the display of the date/time without affecting the order in which MySQL orders the rows. You could use DATE_FORMAT() in your SQL query or one of PHP's date formatting functions like date().

    Hope this helps.

  3. #3
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could preferably do it with MySQL, but if you ever feel like making a universal php function, this should work:

    PHP Code:
    function convertTime $military )
    {
    $x explode(":"$military);
      if ( 
    $x[0] <= 12 )
      return 
    $military;
      if ( 
    $x[0] == 00 )
      return ( 
    $x[0] + 12 ).':'.$x[1].':'.$x[2];
      if ( 
    $x[0] > 12 )
      return ( 
    $x[0] - 12 ).':'.$x[1].':'.$x[2];


  4. #4
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Maine
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set your date field to tinytext. Insert this into that field:

    PHP Code:
    date("F j, Y, g:i:s a"); 
    It's extremely easy - the PHP engine does all the converting for you.

    For formatting instructions, visit here:

    http://us2.php.net/manual/en/function.date.php


  5. #5
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think he was more concerned about the time, and not the full date.
    also, I tend to let mysql do all the work where it can. that's why I still think the first method ( DATE_FORMAT() ) would be the best alternative.


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
  •