php - 如何在 2 个数组中打印第一个 15 天和下一个 15 天的所有日期?
问题描述
这些是一个月内所有日期的代码。我想制作 2 个间隔为 15 天的数组。
$date = "2018-05";
$list=array();
$month = date('m', strtotime($date));
$year = date('Y', strtotime($date));
for($d=1; $d<=31; $d++) {
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month){
$list[]=date('Y-m-d', $time);
}
}
echo "<pre>"; print_r($list); echo "</pre>";
结果 :
Array
(
[0] => 2018-05-01
[1] => 2018-05-02
[2] => 2018-05-03
[3] => 2018-05-04
[4] => 2018-05-05
[5] => 2018-05-06
[6] => 2018-05-07
[7] => 2018-05-08
[8] => 2018-05-09
[9] => 2018-05-10
[10] => 2018-05-11
[11] => 2018-05-12
[12] => 2018-05-13
[13] => 2018-05-14
[14] => 2018-05-15
[15] => 2018-05-16
[16] => 2018-05-17
[17] => 2018-05-18
[18] => 2018-05-19
[19] => 2018-05-20
[20] => 2018-05-21
[21] => 2018-05-22
[22] => 2018-05-23
[23] => 2018-05-24
[24] => 2018-05-25
[25] => 2018-05-26
[26] => 2018-05-27
[27] => 2018-05-28
[28] => 2018-05-29
[29] => 2018-05-30
[30] => 2018-05-31
)
而不是这个如何制作 2 个数组。每月的前 15 天,然后第二个数组将是接下来的 15 天?
解决方案
试试这个。我已经尽可能多地解释了代码。你的问题太笼统了,所以我不能准时回答这个问题。您问如何创建输出,这正是如何创建的。
<style>
th, td { border: 1px solid gray; }
th { height:20px;}
/* here I'm giving borders to the grid, and a default height so 2nd line doesn't collapse */
</style>
<?php
//settings:
$month = 5;
$year = 2018;
$ymd = $year . '-' . str_pad($month,2,"0",STR_PAD_LEFT); //this trows 2018-05-01
$lastDay = date('d', strtotime( $ymd . '-01 + 1 month - 1 day')); //I calculate the last day of the month (in this case, 31)
//title:
echo date('M Y', strtotime($ymd)); //print month and year (May 2018)
//space:
echo '<br><br>';
//first table:
makeTable(1,15); //call to the function below, days 1 to 15
//space:
echo '<br><br>';
//2nd table:
makeTable(16,$lastDay); //call to the function below, days 15 to last day
function makeTable($from,$to){
global $month;
//begin table:
echo '<table>';
echo '<tr>';
//first row with dates:
for($d=$from; $d<=$to; $d++) {
//here I create each cell of first row
echo '<th>' . str_pad($d,2,"0",STR_PAD_LEFT) . '-' . str_pad($month,2,"0",STR_PAD_LEFT) . '</th>';
}
echo '</tr><tr>';
//second row blank:
for($d=$from; $d<=$to; $d++) {
//same as above but without text inside the cell
echo '<th> </th>';
}
//end table:
echo '</tr>';
echo '</table>';
}
?>
推荐阅读
- python - 无法对分类数据执行 fit_transform
- javascript - GraphQL 突变中的 onError
- arrays - 如何将一个非常大的numpy数组保存为图像,尽可能少地加载到内存中
- ios - 通过 iOS 上 Mapbox 中的按钮在当前位置添加 Maker
- algorithm - 如何通过迭代解决递归关系
- powershell - Powershell 脚本在结果中缺少属性
- swift - Firebase Swift:无法保存数据:权限被拒绝
- html - 在 wtforms SubmitField 中使用图标?
- javascript - 通过给定索引解析具有嵌套对象的数组,获取它们的名称
- c# - 如何修复索引超出范围错误?