Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    time format in dropdown box

    With the coding below I display 24 hours in a dropdown list

    coding:
    PHP Code:
    <select id='time'>
    <?php for($i 1$i <= 24$i++): ?>
        <option value="<?= $i?>"><?= date("h.iA"strtotime("$i:00")); ?></option>
    <?php endfor; ?>
    </select>
    outputs :

    Code:
    <select id='time'>
        <option value="1">01.00AM</option>
        <option value="2">02.00AM</option>
        <option value="3">03.00AM</option>
        <option value="4">04.00AM</option>
        <option value="5">05.00AM</option>
        <option value="6">06.00AM</option>
        <option value="7">07.00AM</option>
        <option value="8">08.00AM</option>
        <option value="9">09.00AM</option>
        <option value="10">10.00AM</option>
        <option value="11">11.00AM</option>
        <option value="12">12.00PM</option>
        <option value="13">01.00PM</option>
        <option value="14">02.00PM</option>
        <option value="15">03.00PM</option>
        <option value="16">04.00PM</option>
        <option value="17">05.00PM</option>
        <option value="18">06.00PM</option>
        <option value="19">07.00PM</option>
        <option value="20">08.00PM</option>
        <option value="21">09.00PM</option>
        <option value="22">10.00PM</option>
        <option value="23">11.00PM</option>
        <option value="24">12.00AM</option>
    </select>
    Which works perfectly, however I don't want to display AM (ante meridiam) and PM (post meridiam). Instead I want the times to be displayed as 00:00 till 23:00. How can I alter the coding ?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    Try this:
    PHP Code:
    <select id='time'>
    <?php for($i 1$i <= 24$i++): ?>
        <option value="<?= $i?>"><?= date("H.i"strtotime("$i:00")); ?></option>

    <?php endfor; ?>
    </select>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post
    it gives me the same results as the am/pm output

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    Quote Originally Posted by docock View Post
    it gives me the same results as the am/pm output
    Then your doing something wrong
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by sunfighter View Post
    Then your doing something wrong
    Er no, you are

    OPs:

    PHP Code:
    <select id='time'>
    <?php for($i 1$i <= 24$i++): ?>
        <option value="<?= $i?>"><?= date("h.iA"strtotime("$i:00")); ?></option>
    <?php endfor; ?>
    </select>
    Yours:

    PHP Code:
    <select id='time'>
    <?php for($i 1$i <= 24$i++): ?>
        <option value="<?= $i?>"><?= date("H.i"strtotime("$i:00")); ?></option>

    <?php endfor; ?>
    </select>
    You removed A from the date() parameters. That only REMOVES the AM/PM thing.

    If you look here, you can see that for time, the correct parameters are: G and i like this:

    PHP Code:
    <select id='time'>
    <?php for($i 1$i <= 24$i++): ?>
        <option value="<?= $i?>"><?= date("G.i"strtotime("$i:00")); ?></option>

    <?php endfor; ?>
    </select>
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,316
    Thanks
    23
    Thanked 613 Times in 612 Posts
    Er no, you are
    No I'm not!
    Your using
    date("G.i", strtotime("$i:00")) I'm using
    date("H.i", strtotime("$i:00"))

    The difference:
    G 24-hour format of an hour without leading zeros 0 through 23
    H 24-hour format of an hour with leading zeros 00 through 23

    Same thing (almost )

    docock said "it gives me the same results as the am/pm output" It doesn't, it gives time in the 24 hour format.

    The only thing that might be changed is
    <?php for($i = 1; $i <= 24; $i++): ?>
    to
    <?php for($i = 0; $i <= 23; $i++): ?> to run zero hundred to 23 hundred hours.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,343
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by sunfighter View Post
    No I'm not!
    Your using
    date("G.i", strtotime("$i:00")) I'm using
    date("H.i", strtotime("$i:00"))

    The difference:
    G 24-hour format of an hour without leading zeros 0 through 23
    H 24-hour format of an hour with leading zeros 00 through 23

    Same thing (almost )
    Urgh.. how on earth did I miss that? Good spot sunfighter

    I personally think that using the date function in there is the wrong way to go anyway to be honest, he's using strtotime() to create a timestamp but then using it in date() which seems a bit whacked out if you ask me. This is how I'd do it (untested though lol):

    PHP Code:
    <select id='time'>
    <?php for($i 1$i 24$i++):
    $i = ($i 10)? "0$i"$i//Leading zeros
        
    <option value="<?= $i; ?>"><?php print "$i:00"?></option>
    <?php endfor; ?>
    </select>
    No complex date() calls in there which from previous projects can act differently depending o php versions etc.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •