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


get all days and dates for the selected month of a year

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

<?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>

我希望我能解释我的自我。

您可以使用

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>

另一种方法是使用 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

你可以试试这样的东西

$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>';
}  

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

时间戳的有效范围通常从 1901 年 12 月 13 日星期五 20:45:54 GMT 到 2038 年 1 月 19 日星期二 03:14:07 GMT。(这些日期对应于 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() 函数不支持翻译的区域设置,因此您可以使用关联数组来获取另一种语言的翻译。

下面的完整解决方案。

我确定给定月/年组合中的天数。然后,我循环查看日期并同时创建所需的两个行。

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

    <?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

试试这个脚本:

// 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>';

在您的情况下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>

一个 DateTime 方法,用于构建 date -> day 的 assoc 数组。

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>