Jump to content

pulling hour and minutes from datetime


bradkenyon

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.