如何登记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
这是我的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);
发布于 2018-06-19 05:32:56
你能试试这个吗:
$begin = strtotime('2018-06-04');
$end = strtotime('2018-06-10');
while($begin < $end){
$begin = $begin +84600;
echo date('Y-m-d', $begin) . ' ';
}
发布于 2018-06-19 05:40:40
如果理解正确,则以下内容应该会产生您预期的结果:
// 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网站上找到,并归功于该帖子的作者。
https://stackoverflow.com/questions/50917356
复制相似问题