note 84123 deleted from function.date by tularis

From: Date: Thu, 09 Feb 2012 21:00:46 +0000
Subject: note 84123 deleted from function.date by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186005@lists.php.net to get a copy of this message
Note Submitter: Kavi Siegel ---- I wrote the following function to show a series of drop down boxes to select the 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 = ""){ if($timestamp == ""){ $timestamp = time(); } $months = array(null, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); unset($months[0]); print_r($months); $out = '<select name="month">'; foreach($months as $key => $month){ if($month == date('M', $timestamp)){ $out .= '<option value="'.$key.'" selected="selected">'.$month.'</option>'; }else{ $out .= '<option value="'.$key.'">'.$month.'</option>'; } } $out .= '</select><select name="days">'; for($i = 1; $i <= 32; $i++){ if($i == date('j', $timestamp)){ $out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>'; }else{ $out .= '<option value="'.$i.'">'.$i.'</option>'; } } $out .= "</select><select name='year'>"; for($i = date('Y'); $i >= 1970; $i--){ if($i == date('Y', $timestamp)){ $out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>'; }else{ $out .= '<option value="'.$i.'">'.$i.'</option>'; } } $out .= "</select>"; return $out; } ?> Usage is simple: <?php echo chooseDate(); // Will select current date echo chooseDate(1149566400); // Will select June 6th, 2006 ?>

« previous php.notes (#186005) next »