SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    storing an abbreviated value then retrieve it in full

    Hi,

    In this case, value is time.

    I need to store time value in database and when they are called, they need to be interpreted as time and display in time format.

    Below is a select with options are clsoing time of a listing.

    My mission is stored them when user select, then compare them with server time, further more, display them on the screen for user preference.

    Code HTML4Strict:
    <select name="hour" id="hour">
    	<option selected="selected">Choose closing time</option>
    	<option value="1am">1:00 am</option>
    	<option value="2am">2:00 am</option>
    	<option value="3am">3:00 am</option>
    	<option value="4am">4:00 am</option>
    	<option value="5am">5:00 am</option>
    	<option value="6am">6:00 am</option>
    	<option value="7am">7:00 am</option>
    	<option value="8am">8:00 am</option>
    	<option value="9am">9:00 am</option>
    	<option value="10am">10:00 am</option>
    	<option value="11am">11:00 am</option>
    	<option value="12am">12:00 am</option>
    	<option value="1pm">1:00 pm</option>
    	<option value="2pm">2:00 pm</option>
    	<option value="3pm">3:00 pm</option>
    	<option value="4pm">4:00 pm</option>
    	<option value="5pm">5:00 pm</option>
    	<option value="6pm">6:00 pm</option>
    	<option value="7pm">7:00 pm</option>
    	<option value="8pm">8:00 pm</option>
    	<option value="9pm">9:00 pm</option>
    	<option value="10pm">10:00 pm</option>
    	<option value="11pm">11:00 pm</option>
    	<option value="12pm">12:00 pm</option>	
    </select>

    Any solution appreciated.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand? You want e.g. 1am to become 1:00 AM? If that's the case then this should do it:

    Code PHP:
    <?php
    $string = '1am';
     
    preg_match('/^(\d{1,2})(am|pm)$/i', $string, $matches);
     
    echo $matches[1] . ':00 ' . strtoupper($matches[2]);
    ?>

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <select id=hour name=hour>
        <
    option value="13">12:00 am</option>
        <
    option value="14">1:00 pm</option
    </
    select
    I would say you need to keep your return values as close as possible to your intended final use as possible.

    An hour can best be handled as a number.

    On the backend, when the form is posted you can then more easily:

    1 check that hour is only an integer between 1 and 24

    for example:
    PHP Code:
    if ( $hour >&& $hour <= 24 ) {
    echo 
    "good hour " date("F j, Y, g:i a"mktime0,0$hour,12,3,2008)); 

    2 you can then use that in your time check or to select things from your database

    "select things from yourTable where datefield > 2008-03-12 $hour:00:00 " ;


    PHP Code:
        <option value="12pm">12:00 pm</option
    Using "12pm" as the value means you then have to strip off the "pm", presumably after working out which 12 it was.

    The same would be true for other date and time elements.
    PHP Code:
    <option value="2008-03-12">Thu 3 December</option
    HTH


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
  •