PHP中的日历导航(按周)

时间:2015-10-04 02:08:54

标签: php calendar strtotime

我正在制作一个也会显示一周的日历"查看"。

上周"有一些问题" "下周" NAV ...

以下是我所拥有的:

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

截至10月底(2015年)和11月初分别在周六和周日(分别)落下当月和下个月的精彩作品!

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

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

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

任何人都有一个干净的解决方案?

非常感谢!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在这种情况下,这可能很有用。它包装核心PHP DateTime类并公开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

每个方法都返回NavigableDate的实例。因此,您可以通过任何方法链接日期返回的方法。需要注意的一点是导航方法已经重置了* params,它的名称将时间重置为00:00:00或者每月的第1天。有关这方面的更多信息,请访问plz https://packagist.org/packages/ishworkh/navigable-date

答案 2 :(得分:-1)

我使用了以下内容:

-?compound2atom(3+4,Y).
Y='3+4'.