如何在2个数组中首先打印15天的所有日期和接下来的15天?

时间:2018-06-06 14:57:51

标签: php datetime

这些是一个月内所有日期的代码。我想制作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天?

1 个答案:

答案 0 :(得分:0)

试试这个。我尽可能地解释了代码。你的问题太宽泛了,所以我不能准时回答这个问题。您询问如何创建输出,这正是如何。

输出几乎与您提出的相同: enter image description here

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