Skip to content

Instantly share code, notes, and snippets.

@tjlytle
Created March 19, 2011 17:54
Show Gist options
  • Save tjlytle/877663 to your computer and use it in GitHub Desktop.
Save tjlytle/877663 to your computer and use it in GitHub Desktop.
Example of using PHP's Date objects to generate a select element.
<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