获取一年中所选月份的所有日期和日期

时间:2014-12-12 13:22:05

标签: php date

如何获取所选年份的选定月份的日期和日期,并在表格中显示。 例如: 我到目前为止已经尝试过了。

<?php
$num_of_days = cal_days_in_month(CAL_GREGORIAN, 9, 2003);
for( $i=1; $i<= $num_of_days; $i++)
    $dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
/*echo "<pre>";
print_r($dates);
echo "</pre>";*/
?>
<table>
    <tr>
<?php
foreach($dates as $date){
    echo"<td>".$date."</td>";
}
?>
    </tr>
</table>

这为我执行了这段代码。

<table>
    <tbody><tr>
<td>01</td><td>02</td><td>03</td><td>04</td><td>05</td><td>06</td><td>07</td><td>08</td><td>09</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td>    </tr>
</tbody></table>

但我希望在日期行下方另行。 哪个应显示与该日期相关的日期?

白天我的意思是:星期一,星期二,星期三等 按日期我的意思是:1,2,3,4等

所以就像

<tr><td>1</td><td>2</td><td>3</td><td>4</td>
<tr><td>Mon</td><td>Tues</td><td>Wed</td><td>Thursday</td>

我希望我能解释一下自己......

8 个答案:

答案 0 :(得分:10)

您可以使用date('l')获取相应的日期名称:

<?php
$date = '2003-09-01';
$end = '2003-09-' . date('t', strtotime($date)); //get end date of month
?>
<table>
    <tr>
    <?php while(strtotime($date) <= strtotime($end)) {
        $day_num = date('d', strtotime($date));
        $day_name = date('l', strtotime($date));
        $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
        echo "<td>$day_num <br/> $day_name</td>";
    }
    ?>
    </tr>
</table>

答案 1 :(得分:9)

另一种方法可能是使用DateTime对象:

$aDates = array();
$oStart = new DateTime('2014-12-01');
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));

while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {
    $aDates[] = $oStart->format('D d');
    $oStart->add(new DateInterval("P1D"));
}

然后打印:

foreach ($aDates as $day) {
    echo $day;
}

有关格式参数的详细信息,请参阅:http://php.net/manual/en/function.date.php

答案 2 :(得分:4)

您可以尝试这样的事情

$myYearMonth = '2003-09';

$start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));
$end = new DateTime(date('Y-m-t', strtotime($myYearMonth)));

$diff = DateInterval::createFromDateString('1 day');
$periodStart = new DatePeriod($start, $diff, $end);

foreach ( $periodStart as $dayDate ){
  echo '<td>'.$dayDate->format( "d\n" ).'</td><td>'.$dayDate->format( "l\n" ).'</td>';
}  

答案 3 :(得分:3)

您不应该使用date()函数,因为根据PHP文档,日期范围限制在1970-2038:

  

时间戳的有效范围通常是从格林尼治标准时间1901年12月13日20:45:54到格林威治标准时间2038年1月19日星期二03:14:07。 (这些是与32位有符号整数的最小值和最大值对应的日期)。但是,在PHP 5.1.0之前,在某些系统(例如Windows)上,此范围仅限于01-01-1970至19-01-2038。

使用DateTime()课程,您可以使用以下功能检索给定月份所需的信息。这使用DateTime::format()获取英文日期名称(无本地化)。

function getMonth($year, $month) {

    // this calculates the last day of the given month
    $last=cal_days_in_month(CAL_GREGORIAN, $month, $year);

    $date=new DateTime();
    $res=Array();

    // iterates through days
    for ($day=1;$day<=$last;$day++) {
            $date->setDate($year, $month, $day);

            $res[$day]=$date->format("l");
    }
    return $res;
}

这将返回一个关联的数组,如下所示:

$res=getMonth(2015, 2);
print_r($res);


Array
(
[1] => Sunday
[2] => Monday
[3] => Tuesday
[4] => Wednesday
[5] => Thursday

[...]

)

要在两行表中输出数据,可以使用以下代码:

<?php
echo '<table><tr><td>'.implode('</td><td>', array_keys($res)).'</td></tr>';
echo '<tr><td>'.implode('</td><td>', $res).'</td></tr></table>';

由于Datetime::format()函数不支持翻译的语言环境,您可以使用关联数组来获取另一种语言的翻译。

答案 4 :(得分:3)

以下完整解决方案。

我确定给定月/年组合中的天数。然后我循环过去几天同时创建两个必需的行。

完成后,这两行将包装在一个表中并返回给调用者。

    <?php

    echo buildDate(12, 2017);

    function buildDate($month, $year)
    {
        // start with empty results
        $resultDate = "";
        $resultDays = "";
        // determine the number of days in the month
        $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        for ($i = 1; $i <= $daysInMonth; $i++)
        {
            // create a cell for the day and for the date
            $resultDate .= "<td>".sprintf('%02d', $i)."</td>";
            $resultDays .= "<td>".date("l", mktime(0, 0, 0, $month, $i, $year))."</td>";
        }
        // return the result wrapped in a table
        return "<table>".PHP_EOL.
        "<tr>".$resultDate."</tr>".PHP_EOL.
        "<tr>".$resultDays."</tr>".PHP_EOL.
        "</table>";
    }

    ?>

phpfiddle链接:http://phpfiddle.org/main/code/ffjm-hqsu

答案 5 :(得分:2)

试试这个脚本:

// Day of month, e.g. 2014-12-14 if you need the table for december 2014
$date = time();

// Array containing the dates and weekdays
$days = array();
// loop to populate the array
for(
    $day = strtotime('midnight', strtotime(date('1 F Y', $date)));   /* first day of month */
    $day < strtotime(date('1 F Y', strtotime('next month', $date))); /* first day of next month */
    $day = strtotime('next day', $day)
){
    // insert current day into the array
    $days[date('d', $day)] = date('l', $day);
}

// print the row containing all day numbers
echo '<tr><td>'.implode('</td><td>', array_keys($days)).'</td></tr>';
// print the row containing all weekday names
echo '<tr><td>'.implode('</td><td>', $days).'</td></tr>';

答案 6 :(得分:1)

在您的情况下date('D', strtotime($date))应该有效,但您需要格式为yyyy-mm-dd的日期

我做了一些测试,结果如下:

for( $i=1; $i<= $num_of_days; $i++){
    $dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
    $d = "2003-09-".$i;
    $days[] = date('D', strtotime($d));
}

添加了另外tr天:

<tr>
<?php
foreach($days as $day){
    echo"<td>".$day."</td>";
}
?>
</tr>

答案 7 :(得分:1)

构建日期关联数组的DateTime方法 - &gt;一天。

2015年12月的结果如下:

array(31) {
    [1] = string(7) "Tuesday"
    [2] = string(9) "Wednesday"
    [3] = string(8) "Thursday"
    [4] = string(6) "Friday"
    [5] = string(8) "Saturday"
    [6] = string(6) "Sunday"
    [7] = string(6) "Monday"
    [8] = string(7) "Tuesday"
    [9] = string(9) "Wednesday"
    [10] = string(8) "Thursday"
    [11] = string(6) "Friday"
    [12] = string(8) "Saturday"
    [13] = string(6) "Sunday"
    [14] = string(6) "Monday"
    [15] = string(7) "Tuesday"
    [16] = string(9) "Wednesday"
    [17] = string(8) "Thursday"
    [18] = string(6) "Friday"
    [19] = string(8) "Saturday"
    [20] = string(6) "Sunday"
    [21] = string(6) "Monday"
    [22] = string(7) "Tuesday"
    [23] = string(9) "Wednesday"
    [24] = string(8) "Thursday"
    [25] = string(6) "Friday"
    [26] = string(8) "Saturday"
    [27] = string(6) "Sunday"
    [28] = string(6) "Monday"
    [29] = string(7) "Tuesday"
    [30] = string(9) "Wednesday"
    [31] = string(8) "Thursday"
}

获取所需表格的完整代码:

<?php
// Get an array of days
$arrayDays = getDays(12, 2015);

// Function to get an array of days
function getDays($month, $year){
   // Start of Month
   $start = new DateTime("{$year}-{$month}-01");
   $month = $start->format('F');

   // Prepare results array
   $results = array();

   // While same month
   while($start->format('F') == $month){
      // Add to array
      $day              = $start->format('l');
      $date             = $start->format('j');
      $results[$date]   = $day;

      // Next Day
      $start->add(new DateInterval("P1D"));
   }

   // Return results
   return $results;
}
?>


<!-- Output the Table -->
<table>
   <tr>
      <?php foreach (array_keys($arrayDays) as $someDate): ?>
         <td><?= $someDate; ?></td>
      <?php endforeach; ?>
   </tr>
   <tr>
      <?php foreach (array_values($arrayDays) as $someDate): ?>
         <td><?= $someDate; ?></td>
      <?php endforeach; ?>
   </tr>
</table>