相对日期计算关闭


Relative Date calculation off

我正试图计算相对于某个日期的日期,但我得到一些非常不寻常的回应。有人能解释一下我做错了什么吗?如果重要的话,我是美国科技专家。

 <?php
        $firstweek_firsttime = date('D M j', strtotime("June 2016 first Sunday"));//June 19th 2016
        $firstweek_lasttime = date('D M j', strtotime("June 2016 second Saturday"));
    $ret=array(
        "Session #1. The week of ".$firstweek_firsttime." to ".$firstweek_lasttime." - ",
        "Session #2. The week of ".date('D M j', strtotime("$firstweek_firsttime next Sunday"))." to ".date('D M j', strtotime("$firstweek_lasttime next Saturday"))." - ",
        "Session #3. The week of ".date('D M j', strtotime("$firstweek_firsttime +10 day"))." to ".date('D M j', strtotime("$firstweek_lasttime +10 day"))." - "
        );

        ?>
        <ul>
        <?php
        foreach($ret as $wk)
        {
        ?>
            <li><?php echo($wk);?></li>
        <?php           
        }
?>

我得到了什么:

The week of Sun Jun 19 to Sat Jun 18 -
The week of Thu Jan 1 to Thu Jan 1 -
The week of Wed Jul 1 to Tue Jun 30 -

目标:

The week of Sun Jun 19 to Sat Jun 25 - 
The week of Sun Jun 26 to Sat Jul 2 - 
The week of Sun Jul 3 to Sat Jul 9 - 

这对我很有用。

"你"所在的位置与你的日期无关,除非你设置了时区。日期/时间根据服务器位置设置。

这有点麻烦,如果我能找到更好的方法,我会更新我的答案。

strtotime("$firstweek_firsttime");相当于写入strtotime("Sun Jun 5");将输出1433635200(今天,实际上是Sun Jun 7 2015 00:00:00),因为没有指明年份,服务器默认为当前年份

strtotime("next sunday");将输出1441497600(截至今天,等于Sun Sep 6 2015 00:00:00

strtotime("$firstweek_firsttime next sunday");是无效的标记,将不输出任何内容
因此,由于时间戳为空,日期将自动设置为1970年1月1日
strtotime("$firstweek_lasttime next Saturday")

也是如此

strtotime("$firstweek_firsttime +10 days")strtotime("Sun Jun 5 +10 days")相同
如果没有年份,服务器默认为当前年份,并将其写入strtotime("Sun Jun 7 2015 +10 days"),因为6月7日是2015年6月的第一个星期日
strtotime("$firstweek_lasttime +10 day")

也是如此

话虽如此……这个问题的简单解决方案是将年份添加到$firstweek_firsttime和$firstweek_lasttime的日期格式中。这将使你的约会保持在你期望的年份,就像这样。

<?php
    $firstweek_firsttime = date('D M j Y', strtotime("June 2016 first Sunday")); // Sun Jun 5 2016
    $firstweek_lasttime = date('D M j Y', strtotime("June 2016 second Saturday"));

如果你不想向浏览器输出年份,只需将第一个数组项更改为…
"Session #1. The week of ".date('D M j', strtotime("$firstweek_firsttime"))." to ".date('D M j', strtotime("$firstweek_lasttime"))." - ",

参考

修改日期