php 如何在php中获取上周(星期二或任何其他日期)的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:55:19  来源:igfitidea点击:

how to get the date of last week's (tuesday or any other day) in php?

phpdate

提问by lock

I think its possible but i cant come up with the right algorithm for it.

我认为这是可能的,但我无法想出正确的算法。

What i wanted to do was:

我想做的是:

If today is monday feb 2 2009, how would i know the date of last week's tuesday? Using that same code 2 days after, i would find the same date of last week's tuesday with the current date being wednesday, feb 4 2009.

如果今天是 2009 年 2 月 2 日星期一,我怎么知道上周星期二的日期?2 天后使用相同的代码,我会找到上周星期二的同一日期,当前日期是 2009 年 2 月 4 日星期三。

采纳答案by cg.

I know there is an accepted answer already, but imho it does not meet the second requirement that was asked for. In the above case, strtotime would yield yesterday if used on a wednesday. So, just to be exact you would still need to check for this:

我知道已经有一个可接受的答案,但恕我直言,它不符合要求的第二个要求。在上述情况下,如果在星期三使用 strtotime 将在昨天产生。所以,确切地说,你仍然需要检查这个:

$tuesday = strtotime('last Tuesday');
// check if we need to go back in time one more week
$tuesday = date('W', $tuesday)==date('W') ? $tuesday-7*86400 : $tuesday;


As davil pointed out in his comment, this was kind of a quick-shot of mine. The above calculation will be off by one once a year due to daylight saving time. The good-enough solution would be:

正如 davil 在他的评论中指出的那样,这是我的一个快速拍摄。由于夏令时,上述计算将每年减少一次。足够好的解决方案是:

$tuesday = date('W', $tuesday)==date('W') ? $tuesday-7*86400+7200 : $tuesday;

If you need the time to be 0:00h, you'll need some extra effort of course.

如果您需要时间为 0:00h,您当然需要一些额外的努力。

回答by smo0f

Most of these answers are either too much, or technically incorrect because "last Tuesday" doesn't necessarily mean the Tuesday from last week, it just means the previous Tuesday, which could be within the same week of "now".

大多数这些答案要么太多,要么在技术上不正确,因为“上周二”并不一定意味着上周的周二,它只是意味着前一个周二,可能与“现在”在同一周内。

The correct answer is:

正确答案是:

strtotime('tuesday last week')

回答by Hyman Sleight

PHP actually makes this really easy:

PHP 实际上让这一切变得非常简单:

echo strtotime('last Tuesday');

See the strtotimedocumentation.

请参阅strtotime文档。

回答by Hyman Sleight

Working solution:

工作解决方案:

$z = date("Y-m-d", strtotime("last Saturday"));
$z = (date('W', strtotime($z)) == date('W')) ? (strtotime($z)-7*86400+7200) : strtotime($z);
print date("Y-m-d", $z);

回答by gregjor

// test: find last date for each day of the week
foreach (array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') as $day) {
    print $day . " => " . date('m/d/Y', last_dayofweek($day)) . "\n";
}

function last_dayofweek($day)
{
    // return timestamp of last Monday...Friday
    // will return today if today is the requested weekday
    $day = strtolower(substr($day, 0, 3));
    if (strtolower(date('D')) == $day)
        return strtotime("today");
    else
        return strtotime("last {$day}");
}

回答by Dipak Burnwal

<?php 
    $currentDay = date('D');
    echo "Today-".$today = date("Y-m-d");
    echo "Yesterday-".$yesterday = date("Y-m-d",strtotime('yesterday'));
    echo "Same day last week-".$same_day_last_week = date("Y-m-d",strtotime('last '.$currentDay));
?>

回答by Hologos

Do not use manual calculation, use DateTime object instead. It has proper implementation, takes into account leap years, yeap seconds, etc.

不要使用手动计算,而是使用 DateTime 对象。它有适当的实现,考虑了闰年,是秒等。

$today = new \DateTime();
$today->modify('tuesday last week');

The modify method modifies the date relative to it's state, so if you set the date to a different date, it calculates it relative to it.

modify 方法相对于它的状态修改日期,所以如果你将日期设置为不同的日期,它会相对于它计算它。

$date = new \DateTime('2020-01-01');
echo $date->format('Y-m-d'); // 2020-01-01

$date->modify('tuesday last week');
echo $date->format('Y-m-d'); // 2019-12-24

回答by Hologos

you forgot strtotime for second argument of date('W', $tuesday) hmm.

你忘记了 date('W', $tuesday) 的第二个参数的 strtotime 嗯。

convert $tuesday to timestamp before "$tuesday-7*86400+7200"

将 $tuesday 转换为 "$tuesday-7*86400+7200" 之前的时间戳

mde.

米德。