【问题标题】:PHP Dividing the month into weeks to daysPHP将月份划分为几周到几天
【发布时间】:2015-12-02 11:37:18
【问题描述】:

我想把每个月分成如图所示的天数:

我写了一些代码,但我有这样的东西:

<?php 
$start_date = date('Y-m-d', strtotime('2015-12-28'));
$end_date = date('Y-m-d', strtotime('2018-01-01'));
$i=1;
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d',       strtotime($date. ' + 7 days'))) {
    echo getWeekDates($date, $start_date, $end_date, $i);
    echo "\n";
    $i++;
}

function getWeekDates($date, $start_date, $end_date, $i) {
    $week =  date('W', strtotime($date));
    $year =  date('Y', strtotime($date));
    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); 
    if($from < $start_date) $from = $start_date;
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   
    if($to > $end_date) $to = $end_date;
    echo "$i. od ".$from." do ".$to.'<br>';
}
?>

输出:

1. od 2015-12-28 do 2016-01-03
2. od 2016-01-04 do 2016-01-10
3. od 2016-01-11 do 2016-01-17
4. od 2016-01-18 do 2016-01-24

我不知道如何排除每月一周有 1、2、3、4、5 或 6 天之类的例外情况......

【问题讨论】:

  • 无论你做什么,在内部将日期表示为字符串都会很痛苦

标签: php time calendar days


【解决方案1】:

您可以使用/修改this class 来实现您想要的。

对代码进行了很多注释以理解逻辑。

这是打印日历的方法。

include 'calendar.php';

$calendar = new Calendar();

echo $calendar->show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多