php-查找去年每周同一天的日期
因此,在PHP中,我试图返回基于一周中同一天的去年的日期值。 例:(星期一)2011-12-19输入应返回(星期一)2010-12-20 我只是简单地用-364来做,但在闰年时就失败了。我遇到了另一个函数:php-查找去年每周同一天的日期,php,date,Php,Date,因此,在PHP中,我试图返回基于一周中同一天的去年的日期值。 例:(星期一)2011-12-19输入应返回(星期一)2010-12-20 我只是简单地用-364来做,但在闰年时就失败了。我遇到了另一个函数: $newDate = $_POST['date']; $newDate = strtotime($newDate); $oldDate = strtotime('-1 year',$newDate); $newDayOfWeek = date('w',$oldDate); $oldDayO
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
但是,当您尝试输入一个星期日日期时,这是失败的,因为它会输入一个0(星期日)减去6(去年的星期六日期),并返回一个值T-6。IE输入2011-12-25得到的是2010-12-19,而不是2011-12-26
在php中找到一个好的解决方案,它可以在闰年和一周中的任何一天都有效,这让我有点为难
有什么建议吗
谢谢 如何使用PHP的功能:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
使用strotime()获取去年同一周的日期
使用格式{$year}-W{$week}-{$weekday},如下所示:
echo date("Y-m-d", strtotime("2010-W12-1"));
你可以这样做,只要你不想:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
这就产生了:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
让它更容易:)
@安德鲁·杰克逊工作得很好<代码>2012-12-25给出了
2011-12-27
。
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)