note 89190 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 22 Mar 2009 18:43:16 +0000 |
| Subject: | note 89190 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152287@lists.php.net to get a copy of this message | ||
Note Submitter: Kavi Siegel
----
I posted a function a few months back for a date dropdown box, and I figured I'd post here,
since I've optimized it.
It's such a simple function that run speed isn't significantly effected (this is ten
thousandths of a second faster) but the code is cut in half.
This will return HTML code for 3 dropdown form inputs, to allow a user to select a date. When
provided with a timestamp, that date is selected by default, when none is provided, the current date
is selected.
<?php
function chooseDate($timestamp = ""){
$timestamp = ($timestamp != "") ? $timestamp : time();
$months = array('Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec');
$out = "<select name='month'>\r\n";
foreach($months as $k => $m){
$out .= "<option value='".($k+1)."' ".($m == date('M',
$timestamp) ? "selected='selected'" :
"").">".$m."</option>\r\n";
}
$out .= "</select><select name='days'>\r\n";
for($i = 1; $i <= 32; $i++){
$out .= "<option value='".$i."' ".($i == date('j',
$timestamp) ? "selected='selected'" :
"").">".$i."</option>\r\n";
}
$out .= "</select><select name='year'>\r\n";
for($i = date('Y'); $i >= 1970; $i--){
$out .= "<option value='".$i."' ".($i == date('Y',
$timestamp) ?
"selected='selected'":"").">".$i."</option>\r\n";
}
$out .= "</select><br/>";
return $out;
}
?>
Usage is simple:
<?php
echo chooseDate(); // Will select current date
echo chooseDate(1149566400); // Will select June 6th, 2006
?>