PHP如何将周分为几天

时间:2018-06-18 20:57:15

标签: php

如何争取2018-06-04和2018-06-10之间的所有日期2018-06-04 - 2018-06-10之间的那些日子将是2018-06-05,2018-06- 06,2018-06-07,2018-06-08,2018-06-09,2018-06-11 - 2018-06-17同样如此...

到目前为止,我设法将月份划分为一周(下图),我想进一步将数周划分为几天......

2018-06-04 - 2018-06-10 
2018-06-11 - 2018-06-17 
2018-06-18 - 2018-06-24 
2018-06-25 - 2018-07-01 

http://zakodowac.pl/

这是我的PHP代码,它会在2018-06-04 - 2018-06-10之间生成周块,等等......:

function getMondays($y, $m) {
    return new DatePeriod(
        new DateTime("first monday of $y-$m"),
        DateInterval::createFromDateString('next monday'),
        new DateTime("last day of $y-$m")
    );
}




function list_week_days($year, $month) {

    foreach (getMondays($year, $month) as $monday) {       

            echo  $monday->format(" Y-m-d\n");
        echo '-';
            $sunday  = $monday->modify('next Sunday');
            echo $sunday->format(" Y-m-d\n");
                        echo '<br>';

}

}


list_week_days(2018, 06);

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

$begin = strtotime('2018-06-04');
$end = strtotime('2018-06-10');
while($begin < $end){
   $begin = $begin +84600;
   echo date('Y-m-d', $begin) . ' ';
}

答案 1 :(得分:0)

如果正确理解,那么以下内容应该产生您期望的结果:

// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp 
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday 
for ($i = 0; $i < 7; $i++, $ts += 86400){
    print date("m/d/Y l", $ts) . "\n";
}

如果你想变得更聪明,你可以使用:

// set current date
$date = '04/30/2009';
// parse about any English textual datetime description into a Unix timestamp 
$ts = strtotime($date);
// find the year (ISO-8601 year number) and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
for($i = 1; $i <= 7; $i++) {
    // timestamp from ISO week date format
    $ts = strtotime($year.'W'.$week.$i);
    print date("m/d/Y l", $ts) . "\n";
}

所有这些以及更多信息都可以在this网站上找到,并且可以归功于该帖子的作者。