bradkenyon Posted September 12, 2008 Share Posted September 12, 2008 this is for updating an event day and time: i have a datetime for an event. i want to pull the hour and minutes from the datetime example: 2008-09-12 04:25:00 I want to pull out 04 and 25 Then I have three drop downs, one for hour, one for minute and the other is for AM or PM, based on whether the hour is less than 12, then its AM, otherwise I'll set it to PM. I want to figure out how to automatically set each drop down to the current value already selected in the drop down. This is what I have for the drop down right now: the column in the table to grab the current event_date, which would be: 2008-09-12 04:25:00 I am guessing I need to do some while loop to set the appropriate values as selected when displaying the drop downs on the menu. <?php //hour $h = 1; print '<select name="event_hour">'; while($h <= 12){ print '<option value="'.$h.'">'.$h.'</option>'; $h++; } print '</select> : '; print '<select name="event_minute"> <option value="00">00</option> <option value="15">15</option> <option value="30">30</option> <option value="45">45</option> </select> '; //AM or PM print '<select name="event_am_pm"> <option value="AM">AM</option> <option value="PM">PM</option>'; print '</select>'; ?> Thanks in advance, any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/ Share on other sites More sharing options...
The Little Guy Posted September 12, 2008 Share Posted September 12, 2008 $d = '2008-09-12 04:25:00'; $hour = date("h",strtotime($d)); $min = date("i",strtotime($d)); echo 'Hour: '.$hour.'<br>Minute: '.$min; Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640038 Share on other sites More sharing options...
bradkenyon Posted September 12, 2008 Author Share Posted September 12, 2008 thanks! that solves my first part. now i need to display the hour/minute/am or pm drop downs w/ the time already selected. so if it was 16:15 i want to have these showing up as selected in the drop down: 4:15 PM any takers? thanks so far! Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640043 Share on other sites More sharing options...
The Little Guy Posted September 12, 2008 Share Posted September 12, 2008 do you have AM or PM in your time? If not, you cant do it. Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640044 Share on other sites More sharing options...
bradkenyon Posted September 12, 2008 Author Share Posted September 12, 2008 its military, i plan on throwing a test in there. if less than 12, then its AM if its greater than 12, its PM 09 = AM 21 = PM Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640045 Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 You can just use the UNIX_TIMESTAMP function in your database query instead of strtotime, by the way. In fact, this entire thing can be handled by MySQL... Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640046 Share on other sites More sharing options...
bradkenyon Posted September 12, 2008 Author Share Posted September 12, 2008 really? what about the part of automatically setting the select (drop-down) boxes to what current time is of that event. keep in mind, this is for the update function, to update events. Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640049 Share on other sites More sharing options...
The Little Guy Posted September 12, 2008 Share Posted September 12, 2008 Well since it is in Military, you can do this: $d = '2008-09-12 14:25:00'; $hour = date("h",strtotime($d)); $min = date("i",strtotime($d)); $ante = date("A",strtotime($d)); echo 'Hour: '.$hour.'<br>Minute: '.$min.'<br>Ante: '.$ante; Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640051 Share on other sites More sharing options...
chronister Posted September 12, 2008 Share Posted September 12, 2008 Take a look at this and how I do it for a state display. Notice how as I create the select list, I am checking to see if the state was already selected. This is so if there are errors the options already submitted pre-fill the form so the errors can be corrected. Take this concept and apply it to your situation and how you create your select statements. <select name="state" id="state"> <!-- start the select option --> <option value="" >Choose State </option> <!-- set a blank option --> <?php foreach($state_list as $k=>$v) // loop through the states { // if there is an error and the state that was posted equals the state we are on in our loop if(isset($error) && $state==$k) { echo '<option value="'.$k.'" selected="selected">'.$v.'</option>'."\n"; // add the selected part since this is what was selected before } else { echo '<option value="'.$k.'">'.$v.'</option>'."\n"; // this item in the loop was not selected, so no selected part } } ?> </select> <!-- end the select statement --> For the time part, do strtotime(), that will keep the am pm part in there. Then you will do date('m/d/y h:i:s a', $timestamp); will give you 2/13/08 3:25:24 am When maniuplating dates/times, it is easiest to convert into a unix timestamp either in PHP or pull it as timestamp from the database. Nate Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640053 Share on other sites More sharing options...
bradkenyon Posted September 12, 2008 Author Share Posted September 12, 2008 thanks TLG now i need to select the events current time into the drop downs. so if the hour was: 15:15:00 it will be <select name="hour"> <option value="1"> .... <option value="3" selected> </select> <select name="minute"> <option value="00">00</option> ... <option value="15" selected>15</option> ... </select> same as for AM and PM Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640056 Share on other sites More sharing options...
The Little Guy Posted September 12, 2008 Share Posted September 12, 2008 I changed my date() for hour, since you are using military time. You may also want to change each date value depending on if you are using values with or without a leading zero $d = '2008-09-12 14:25:00'; $hour = date("H",strtotime($d)); $min = date("i",strtotime($d)); $ante = date("A",strtotime($d)); echo '<select>'; for($i=0;$i<24;$i++){ echo '<option value="'.$i.'"'; if($i == $hour){ echo ' selected="selected"'; } echo '>'.$i.'</option>'; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/123981-pulling-hour-and-minutes-from-datetime/#findComment-640063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.