Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取一年中选定月份的所有日期_Php_Date - Fatal编程技术网

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

Php 获取一年中选定月份的所有日期,php,date,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 "</pr

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

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

这为我执行这段代码

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

010203040506070809101112131415161718192021222324252627282930    
但我想在日期行下面再加一行。 哪个应该显示与该日期相关的日期

我指的是星期一、星期二、星期三等。 我所说的日期是指:1、2、3、4等等

那就好像

<?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>
1234
星期四
我希望我能解释一下我自己。

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

$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"));
}

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

foreach ($aDates as $day) {
    echo $day;
}
有关格式参数的更多信息,请参阅:

在您的案例中,
日期('D',strotime($date))
应该可以工作,但您需要日期的格式为
yyyy-mm-dd

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

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

你可以试试这样的

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;
}
$myYearMonth='2003-09';
$start=新日期时间(日期('Y-m-01',标准时间($myYearMonth));
$end=新的日期时间(日期('Y-m-t',strottime($myYearMonth));
$diff=DateInterval::createFromDateString('1天');
$periodStart=newdateperiod($start、$diff、$end);
foreach($periodStart as$dayDate){
回显'.$dayDate->格式(“d\n”)'.$dayDate->格式(“l\n”)';
}  
您不应该使用该函数,因为根据PHP文档,日期范围限制为1970-2038年:

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

使用该类,您可以使用以下函数检索给定月份所需的信息。这用于以英语获取日期名称(无本地化)

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

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"
}

一种DateTime方法,用于构建日期->日期的关联数组

2015年12月的结果如下所示:

<?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>
获取所需表格的完整代码:

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

试试这个脚本:

    <?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>";
    }

    ?>
//如果您需要2014年12月的表格,请填写月份,例如2014-12-14
$date=time();
//包含日期和工作日的数组
$days=array();
//循环以填充数组
为了(
$day=strotime('midnight',strotime(date('1fy',$date));/*月的第一天*/
$day
下面是完整的解决方案

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

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



phpfiddle链接:

与该日相关的日期代表什么?哦,我是说那天和那天不动产。。xD e-g星期一、星期二、星期三等。您可以使用以下命令->day_num不是介于1和31之间的整数吗?今天在朱利安是2457004。谢谢。但这并不是那个日期的正确日期。。我的意思是这个月的第一次约会是从周一开始,但当我检查你的代码时,它是从周二开始的?@SizzlingCode我修改了这个答案,实际上我的第一个想法是使用DateTime类,但下面有一个类似的答案,但这应该行得通谢谢。只是再次检查了你的代码。并在不同的月份进行了测试。结果非常好。。一年中正确日期和月份的正确日期名称。再次感谢。@SizzlingCode我很高兴这有助于如何用这种方法同时打印日期和日期???正如我希望他们显示???只希望日期,而不是月和年的格式。这就是为什么我会得到约会号码。但是随着时间的推移(一天总是从一个月的星期一开始??,您是否在不同的月份测试了代码???如果您在
(CAL_GREGORIAN,2012年12月)中更改日期;
您也需要在
$d=“2003-09-”“$i;”
中更改日期。代码运行得很好。phpfiddle在
sprintf('%02d',$i)中有一些问题)
,仅将其替换为
$i
<?php
echo '<table><tr><td>'.implode('</td><td>', array_keys($res)).'</td></tr>';
echo '<tr><td>'.implode('</td><td>', $res).'</td></tr></table>';
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>
// 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>';
    <?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>";
    }

    ?>