Php 从1号到1号生成每月日期
下面的代码将为每个月生成日期。。如何列出下个月第一天之前的日期 示例1月1日至2月1日,5月1日至6月1日…等Php 从1号到1号生成每月日期,php,date,Php,Date,下面的代码将为每个月生成日期。。如何列出下个月第一天之前的日期 示例1月1日至2月1日,5月1日至6月1日…等 <?php $month = "01"; $year = "2017"; $start_date = "01-".$month."-".$year; $start_time = strtotime($start_date); $end_time = strtotime("+1 month", $start_time); echo "<table border='1'
<?php
$month = "01";
$year = "2017";
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
echo "<table border='1'>";
for($i=$start_time; $i<$end_time; $i+=86400)
{
$list = date('Y-m-d-D', $i);
echo "<tr><td>";
echo $list;
echo "</td><td> </td></tr>";
}
echo "</table>";
?>
首先创建一个函数,这样就可以在不复制代码的情况下再次使用它。阅读里面的注释,看看每一行的作用
<?php
function genMonthTable($month, $year){
/* Get days in month */
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
/* Add 1 extra day to get first day of the next month */
$days+=1;
/* Instantiate DateTime object */
$date = new DateTime();
/* Set date */
$date->setDate($year, $month, 1);
/* Create table */
echo "<table style='border: 1px solid black;'><tbody>";
/* Loop table rows */
for($i = 1; $i <= $days; $i++){
echo "<tr><td>";
echo $date->format('Y-m-d');
echo "</td></tr>";
/* Increate date by 1 day */
$date->modify('+1 day');
}
/* Finish table */
echo "</tbody></table>";
/* Unset DateTime object */
unset($date);
}
/* Generate table for Januari 2017 */
genMonthTable(1, 2017);
/* Generate table for May 2017 */
genMonthTable(5, 2017);
?>
非常感谢兄弟,但所列日期仅为1月1日至月底,而非下个月1日。我希望是1月1日、2日、3日至1日feb@blackrx那很容易。只要做$days+=1代码>在$days=cal\u days\u月(cal\u GREGORIAN,$month,$year)代码>我已经为您编辑了代码。顺便说一句,如果在原始代码中可以这样做吗?@blackrx我不确定。这是一种非常奇怪的方式。我得把它摆弄一下,看看有没有问题myself@blackrx是的,是的。不过你会笑这个的。只需更改$i即可