note 89190 added to function.date

From: Date: Wed, 25 Feb 2009 22:56:41 +0000
Subject: note 89190 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-151137@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 208.69.120.35 Probable Submitter: 24.62.22.134 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/89190 Del: integrated -- https://master.php.net/note/delete/89190/integrated Del: useless -- https://master.php.net/note/delete/89190/useless Del: bad code -- https://master.php.net/note/delete/89190/bad+code Del: spam -- https://master.php.net/note/delete/89190/spam Del: non-english -- https://master.php.net/note/delete/89190/non-english Del: in docs -- https://master.php.net/note/delete/89190/in+docs Del: other reasons-- https://master.php.net/note/delete/89190 Reject -- https://master.php.net/note/reject/89190 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151137) next »