简体   繁体   English

php - 查找去年一周中同一天的日期

[英]php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.因此,在 PHP 中,我试图根据一周中的同一天返回去年的日期值。 EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20. EX:(星期一)2011-12-19 输入应返回(星期一)2010-12-20。

I was just doing it simply by -364 but then that was failing on leap years.我只是简单地按-364 做,但那在闰年就失败了。 I came across another function :我遇到了另一个功能:

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

however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6.但是,当您尝试输入星期日日期时,它会失败,因为它会输入 0(星期日)减去 6(去年日期的星期六),并返回值 T-6。 IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26. IE 输入 2011-12-25 会得到 2010-12-19 而不是 2011-12-26。

I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.我很难在 php 中找到一个适用于闰年和一周中的所有日子的好的解决方案。

Any suggestions?有什么建议?

Thanks!谢谢!

How about this, using PHP's DateTime functionality: 怎么样,使用PHP的DateTime功能:

$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
$newDate = '2011-12-19';

date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);

$dateFormat = 'Y-m-d l w W';

echo "This date: ", date($dateFormat, $newDate), "\n"; 
echo "Old date : ", date($dateFormat, $oldDate);

That gives: 这给了:

This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51

Make it easier :) 让它更容易:)

echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));

Edit: I forgot to write output: :) 编辑:我忘记写输出::)

2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)

Use strtotime() to get a date, for the same week last year. 使用strtotime()获取去年同一周的日期。

Use the format {$year}-W{$week}-{$weekday}, like this: 使用格式{$ year} -W {$ week} - {$ weekday},如下所示:

echo date("Y-m-d", strtotime("2010-W12-1"));

And you can do that for as long back you wan't: 而且你可以做到这一点很长一段时间你不会:

<?php

  for($i = 2011; $i > 2000; $i--)
    echo date("Y-m-d", strtotime($i."-W12-1"));

?>
<?php
    $date = "2020-01-11";
    $newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
    echo $newdate;
?>

ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php参考https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM