Created
March 19, 2011 17:54
-
-
Save tjlytle/877663 to your computer and use it in GitHub Desktop.
Example of using PHP's Date objects to generate a select element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select name="time" id="time"> | |
<?php | |
$now = new DateTime(); | |
$select = new DateTime(); | |
$select->setTime(0,0); | |
$tomorrow = new DateTime('tomorrow'); | |
$tomorrow->setTime(0,0); | |
?> | |
<?php while($select < $tomorrow): ?> | |
<?php $diff = $select->diff($now) ?> | |
<option value='<?php echo $select->format('g:i a')?>'<?php if((0 == $diff->h) AND ($diff->i <= 15)):?>selected<?php endif;?> ><?php echo $select->format('g:i a')?></option> | |
<?php $select->add(new DateInterval('PT30M')); ?> | |
<?php endwhile; ?> | |
</select> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment