php -在去年的同一天找到日期。

时间:2022-10-21 10:24:57

So, in PHP i'm trying to return a date value for last year based on the same day of week. EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.

在PHP中,我尝试返回去年的日期值,基于一周的同一天。例:(星期一)2011-12-19输入应返回(星期一)2010-12-20。

I was just doing it simply by -364 but then that was failing on leap years. I came across another function :

我只是在-364之前做了,但在闰年这是失败的。我遇到了另一个函数:

$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. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.

然而,当您尝试输入一个星期日日期时,这是失败的,因为它是0(星期日)-6(去年星期六),返回值为T-6。输入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!

谢谢!

4 个解决方案

#1


8  

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

#2


4  

$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

#3


2  

Use strtotime() to get a date, for the same week last year.

使用strtotime()获得一个日期,在去年的同一个星期。

Use the format {$year}-W{$week}-{$weekday}, like this:

使用格式{$year} {$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"));

?>

#4


2  

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)

#1


8  

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

#2


4  

$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

#3


2  

Use strtotime() to get a date, for the same week last year.

使用strtotime()获得一个日期,在去年的同一个星期。

Use the format {$year}-W{$week}-{$weekday}, like this:

使用格式{$year} {$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"));

?>

#4


2  

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)