我想在当前日期的下拉列表中显示一个月的日期和日期 - 月份

时间:2015-07-06 06:18:27

标签: php date

我当前的脚本显示当月的所有日期,但我想要总共30个日期,包括下个月的日期:

<?php        

$dateToday = Date("d");             
$currentMonthDigits = Date("m");                
$currentMonthText = Date("M");              
$currentYear = Date("Y");                        

for ( $i=$dateToday; $i<32; $i++ ) {
  $theday = date('d', mktime(0,0,0,0,$i,2000)); 
  if($theday == $dateToday)                     
    echo "<option value='$currentYear-$currentMonthDigits-$theday'>Today</option>";
  else
    echo "<option value='$currentYear-$currentMonthDigits-$theday'>$theday $currentMonthText $currentYear</option>";
}
?> 

2 个答案:

答案 0 :(得分:1)

$date = time();
for ( $i=$dateToday; $i<32; $i++ ) {
    echo date("Y-m-d", $date) . " " .  date("d M Y", $date) . "\n";
    $date = strtotime(date("Y-m-d", $date) . " +1 day" );
}

输出

2015-07-06 06 Jul 2015
...
2015-07-30 30 Jul 2015
2015-07-31 31 Jul 2015
2015-08-01 01 Aug 2015
2015-08-02 02 Aug 2015
...
2015-08-06 06 Aug 2015

Demo

答案 1 :(得分:0)

$dateToday = date('Y-m-d'); 
$date30th = date("Y-m-d",strtotime("+30 days"));
$dateRanges = date_range($dateToday,$date30th); 
$output = '';
foreach($dateRanges as $dateRange) {
    $output .= '<option value='.$dateRange.'>'.$dateRange.'</option>';
}
echo '<select>'.$output.'</select>';
function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {

    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);

    while( $current <= $last ) {

        $dates[] = date($output_format, $current);
        $current = strtotime($step, $current);
    }

    return $dates;
}

您将从stackoverflow本身获得一些提示,请查看此链接 - https://stackoverflow.com/a/9225875/639293