note 84123 modified in function.date by danbrown

From: Date: Sun, 20 Jul 2008 02:41:58 +0000
Subject: note 84123 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141977@lists.php.net to get a copy of this message
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 ?> --was-- 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. 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: echo chooseDate(); // Will select current date echo chooseDate(1149566400); // Will select June 6th, 2006 http://php.net/manual/en/function.date.php

« previous php.notes (#141977) next »