PHP 中的日历导航(按周)

Calendar navigation in PHP (by week)

我正在构建一个日历,它也显示一周 "view"。

"last week" 和 "next week" 导航有问题...

这是我得到的:

/*
   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">

当月(2015 年)10 月底和 11 月初分别在周六和周日(分别),本月和下个月的效果非常好!

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

从 2015 年到 2016 年它会中断..

我使用 strtotime 尝试了几种不同的方法...

有人有干净的解决方案吗?

非常感谢!

不确定您想要什么?上周和下周的星期一的日期?星期天?你有 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";

以上代码理论上应该在 PHP 5.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 class 并公开了 nextDay、previousDay、nextMonth 等方法。因此,按日、月、年浏览日期真的很容易。

https://packagist.org/packages/ishworkh/navigable-date

例如

$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;

给予

Current Date --> 2017-02-23 Date After a week --> 2017-03-02 Date before a week --> 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;

给予

Date before a day from next week date --> 2017-03-01

每个方法 return 都是 NavigableDate 的一个实例。因此,您可以通过任何方法将日期 return 上的方法链接起来。需要注意的一件事是导航方法具有 reset* 参数,根据其名称,将时间重置为 00:00:00 或将天数重置为 1,即每月的第一天。有关这方面的更多信息,请访问, https://packagist.org/packages/ishworkh/navigable-date