PHP中的日历导航(按周)

PHP中的日历导航(按周),php,calendar,strtotime,Php,Calendar,Strtotime,我正在创建一个日历,它还显示一周的“视图” “上周”和“下周”导航有问题 以下是我得到的: /* Date to calc the start and end of week. $thisYear, $thisMonth, $thisDay are passed in */ $combineDate = $thisYear . "-" . $thisMonth . "-" . $thisDay . " 00:00:00"; //$timeString = strtotime($co

我正在创建一个日历,它还显示一周的“视图”

“上周”和“下周”导航有问题

以下是我得到的:

/*
   Date to calc the start and end of week.
   $thisYear, $thisMonth, $thisDay are passed in
*/

$combineDate = $thisYear . "-" . $thisMonth . "-" . $thisDay . " 00:00:00";
//$timeString = strtotime($combineDate); (tried with this format, but same result)

//calculate last week's date
$lastWeek_year = date('Y',strtotime("$combineDate -1 week"));
$lastWeek_month = date('n',strtotime("$combineDate -1 week"));
$lastWeek_day = date('j',strtotime("$combineDate -1 week"));

//calculate next week's date
$nextWeek_year = date('Y',strtotime("$combineDate +1 week"));
$nextWeek_month = date('n',strtotime("$combineDate +1 week"));
$nextWeek_day = date('j',strtotime("$combineDate +1 week"));

[links look like this:]
//last week
<a href="$currentPage?day=$lastWeek_day&month=$lastWeek_month&year=$lastWeek_year">

//next week
<a href="$currentPage?day=$nextWeek_day&month=$nextWeek_month&year=$nextWeek_year">
/*
计算一周开始和结束的日期。
$thisYear、$thisMonth、$thisDay已传入
*/
$combineDate=今年$。"-" . $这个月。"-" . $今天。" 00:00:00";
//$timeString=strottime($combineDate);(尝试使用此格式,但结果相同)
//计算上周的日期
$lastWeek_year=日期('Y',标准时间($combineDate-1周));
$lastWeek_month=日期('n',标准时间($combineDate-1周));
$lastWeek_day=日期('j',标准时间($combineDate-1周));
//计算下周的日期
$nextWeek_year=日期('Y',标准时间($combineDate+1周));
$nextWeek_month=日期('n',标准时间($combineDate+1周));
$nextWeek_day=日期('j',标准时间($combineDate+1周));
[链接看起来像这样:]
//上周
//下周
由于10月底(2015年)和11月初分别是星期六和星期日,因此本月和下个月的工作效果非常好

但它在12月中断,因为1号是星期二(doh!)

从2015年到2016年,它会崩溃

我用strotime尝试了几种不同的方法

谁有干净的解决方案


非常感谢

不确定你想要什么?上周和下周的星期一的日期?星期天?你有DateTime对象吗?这行吗

$date = new DateTime(NULL);

$datey = new DateTime(); $date = $date->setDate($thisYear, $thisMonth, $thisDay)->setTime(0, 0);
$beginLastWeek = $datey->setTimestamp(strtotime("this week", $date->sub(new DateInterval("P1W"))->getTimestamp()))
    ->sub(new DateInterval('P1D'));
$datex = new DateTime(); $date = $date->setDate($thisYear, $thisMonth, $thisDay)->setTime(0, 0);
$beginNextWeek = $datex->setTimestamp(strtotime("this week", $date->add(new DateInterval("P1W"))->getTimestamp()))
    ->sub(new DateInterval('P1D'));

list($lastWeek_year, $lastWeek_month, $lastWeek_day) =
    explode(":", $beginLastWeek->format("Y:m:d"));
list($nextWeek_year, $nextWeek_month, $nextWeek_day) =
    explode(":", $beginNextWeek->format("Y:m:d"));

print "$lastWeek_year-$lastWeek_month-$lastWeek_day\n";
print "$nextWeek_year-$nextWeek_month-$nextWeek_day\n";
上述代码在理论上应适用于PHP5.3,并将星期日视为一周的开始,并给出2001-01-01您将得到:

上周开始日期:2000-12-24 下周开始日期:2001-01-07

$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');

在这种情况下,这可能很有用。它包装了核心PHP DateTime类,并公开了nextDay、previousDay、nextMonth等方法。因此,按天、月、年浏览日期非常容易

例如

$daysInAWeek = 7;

$DateNow = \NavigableDate\NavigableDateFacade::create('2017-02-23');

echo 'Current Date --> ' . $DateNow->format('Y-m-d') . PHP_EOL;

$DateAfterAWeek = $DateNow->daysAfter($daysInAWeek);
echo 'Date After a week --> ' . $DateAfterAWeek->format('Y-m-d') . PHP_EOL;

$DateBeforeAWeek = $DateNow->daysAfter(-$daysInAWeek);
echo 'Date before a week --> ' . $DateBeforeAWeek->format('Y-m-d') . PHP_EOL;
给予

当前日期-->2017-02-23
一周后的日期-->2017-03-02
一周前的日期-->2017-02-16

同样地

方法接受resetTime布尔值,该值决定时间是否必须设置为00:00:00

$PrevDay = $DateAfterAWeek->previousDay(false);
echo 'Date before a day of next week date --> ' . $PrevDay->format('Y-m-d') . PHP_EOL;
给予

从下周开始的前一天日期-->2017-03-01

每个方法都返回
NavigableDate
的一个实例。因此,您可以通过任何方法在日期返回时链接这些方法。需要注意的是,navigate方法具有reset*params,其名称将时间重置为00:00:00,或将天重置为1,即每月的第一天。欲了解更多信息,请访问,

澄清一下:上面的函数中有一个日期字符串(YYYY、MM、DD)。从那以后,我将建立一个为期一周的日历。所以我需要计算一周中第一天(星期日)的日期(YYYY,MM,DD)和一周中最后一天(星期六)的日期。这似乎不太合适。我的代码在PHP 5.6中运行(虽然PHP似乎认为星期一是一周的第一天)。谢谢,但是上面的3行可以用来输出我需要的任何日期字符串。基本上这是你的答案(因为我使用的是DateTime对象)…你只需要增加10天,这样2000-01-01(星期六)+10天=2000-01-11。实际上,你会在整个星期一经过,然后在星期二降落。这似乎与您描述的问题不匹配。当然,我将日期字符串从“10天”更改为“1天”(etc),以符合我的需要。我的观点是,我只需要这三行代码就可以得到我想要的结果。感谢您提供有关datetime对象的提示。如果您可以使用您提到的软件包,通过一些代码示例/代码片段详细说明您的答案,而不仅仅是粘贴链接,那将更有帮助。