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

时间:2022-10-21 10:10:15

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年1月2日星期一,我怎么知道上周的星期二的日期?在2天后使用相同的代码,我会发现上周二的同一天,当前日期是星期三,2009年2月4日。

7 个解决方案

#1


17  

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:

正如戴维尔在评论中指出的那样,这是我的一个快速镜头。由于夏令时,上述计算将每年关闭一次。足够好的解决方案是:

$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,当然你需要额外的努力。

#2


14  

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')

#3


13  

PHP actually makes this really easy:

PHP实际上让这很容易:

echo strtotime('last Tuesday');

See the strtotime documentation.

请参阅strtotime文档。

#4


3  

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

#5


1  

// 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}");
}

#6


0  

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

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

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

在“$ tuesday-7 * 86400 + 7200”之前将$ tuesday转换为时间戳

mde.

#7


0  

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

#1


17  

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:

正如戴维尔在评论中指出的那样,这是我的一个快速镜头。由于夏令时,上述计算将每年关闭一次。足够好的解决方案是:

$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,当然你需要额外的努力。

#2


14  

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')

#3


13  

PHP actually makes this really easy:

PHP实际上让这很容易:

echo strtotime('last Tuesday');

See the strtotime documentation.

请参阅strtotime文档。

#4


3  

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

#5


1  

// 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}");
}

#6


0  

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

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

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

在“$ tuesday-7 * 86400 + 7200”之前将$ tuesday转换为时间戳

mde.

#7


0  

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