简体   繁体   English

PHP如何将星期分成几天

[英]PHP How to divide weeks into days

How to enlist all the days between for instance 2018-06-04 and 2018-06-10 that between 2018-06-04 - 2018-06-10 those days will be 2018-06-05, 2018-06-06, 2018-06-07, 2018-06-08, 2018-06-09, the same goes for 2018-06-11 - 2018-06-17 and so on... 如何征用例如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等

So far I'ive managed to divide month into week chunks (below), I want to further divide weeks into days... 到目前为止,我已经设法将月份分为几周(如下),我想进一步将几周分为几天...

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/ http://zakodowac.pl/

This is my PHP code which produces week chunks above 2018-06-04 - 2018-06-10 and so on...: 这是我的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);

could you try this: 你可以试试这个吗?

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

if correctly understood, then the following should yield the results you expect: 如果正确理解,则以下内容将产生您期望的结果:

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

if you want to be even more clever, you can use: 如果您想变得更聪明,可以使用:

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

All of which, alongside more information, can be found on this website and credit goes to the author of that post. 所有这些以及更多信息都可以在网站上找到,并归功于该文章的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM