获取本周星期一和星期五的日期 (PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1431631/
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-25 02:32:41  来源:igfitidea点击:

Get date for monday and friday for the current week (PHP)

phpdatedatetime

提问by Psyche

How can I get the date for monday and friday for the current week?

如何获得本周的星期一和星期五的日期?

I have the following code, but it fails if current day is sunday or saturday.

我有以下代码,但如果当前日期是星期日或星期六,它会失败。

$current_day = date("N");
$days_to_friday = 5 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("Y-m-d", strtotime("- {$days_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));

回答by Salman A

These strtotime inputs work very well:

这些 strtotime 输入效果很好:

strtotime( "next monday" );
strtotime( "previous monday" );
strtotime( "today" );
strtotime( "next friday" );
strtotime( "previous friday" );

All you need to do is to wrap the logic inside some if statements.

您需要做的就是将逻辑包装在一些 if 语句中。

回答by GDY

Best solution would be:

最好的解决办法是:

$monday = date( 'Y-m-d', strtotime( 'monday this week' ) );
$friday = date( 'Y-m-d', strtotime( 'friday this week' ) );

回答by vascowhite

This question needs a DateTime answer:-

这个问题需要一个 DateTime 答案:-

/**
 * @param String $day
 * @return DateTime
 */
function getDay($day)
{
    $days = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Sunday' => 7];

    $today = new \DateTime();
    $today->setISODate((int)$today->format('o'), (int)$today->format('W'), $days[ucfirst($day)]);
    return $today;
}

Usage:

用法:

var_dump(getDay('Monday')->format('l dS F Y'));
var_dump(getDay('Friday')->format('l dS F Y'));

Output:

输出:

string 'Monday 30th September 2013' (length=26)
string 'Friday 04th October 2013' (length=24)

See it working

看到它工作

回答by karancan

This really depends on how you define a weekbut I came up with this function that will give you the date for the nearest "monday" or "friday" (or any day for that matter):

这真的取决于你如何定义一周,但我想出了这个函数,它会给你最近的“星期一”或“星期五”(或任何一天)的日期:

function closestDate($day){

    $day = ucfirst($day);
    if(date('l', time()) == $day)
        return date("Y-m-d", time());
    else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day)))
        return date("Y-m-d", strtotime('next '.$day));
    else
        return date("Y-m-d", strtotime('last '.$day));

}

Input: a day of the week ("sunday", "Monday", etc.)

输入:一周中的某一天(“星期日”、“星期一”等)

Output: If I asked for the nearest "sunday" and today is:

输出:如果我问最近的“星期日”,今天是:

  1. "Sunday": I will get today's date
  2. "Monday": I will get yesterday's date
  3. "Saturday: I will get tomorrow's date
  1. “星期日”:我会得到今天的日期
  2. “星期一”:我会得到昨天的日期
  3. “星期六:我会得到明天的日期

Hope this helps :)

希望这可以帮助 :)

回答by Baim Wrong

i use :

我用 :

$first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
$last_week_date = date('d F Y', strtotime('next Monday -1 week + 4 days', strtotime('this sunday')));

回答by davidandrew

As the top answer suggests, using PHP's strtotime()function is the easiest way.

正如最佳答案所暗示的那样,使用 PHP 的strtotime()函数是最简单的方法。

However, instead of using if statements as he suggests, you could simply reset back to the previous Sunday and grab the dates you require from there.

但是,您可以简单地重置回上一个星期日并从那里获取所需的日期,而不是像他建议的那样使用 if 语句。

$monday = strtotime('next monday', strtotime('previous sunday'));
$friday = strtotime('next friday', strtotime('previous sunday'));

回答by jgreep

I was looking for a similar thing, except I wanted any Monday, not just this week. This is what I came up with:

我正在寻找类似的东西,除了我想要任何星期一,而不仅仅是本周。这就是我想出的:

function getSunday(DateTime $date){
    $outdate = clone($date);
    $day = $date->format("w");  // get the weekday (sunday is 0)
    $outdate->sub(new DateInterval("P".$day."D")); // subtracting the weekday from the date always gives Sunday 
    return $outdate;
}

It accepts an arbitrary date and gives the Sunday. Then you can easily add back days to get Monday through Saturday.

它接受任意日期并给出星期日。然后,您可以轻松地将天数添加回来以获得周一至周六。

回答by MjrKusanagi

I needed a definition of the current week per ISO 8601. I want Monday to always be defined as the Monday that started this current week.

我需要根据 ISO 8601 定义当前周。我希望星期一始终定义为本周开始的星期一。

The following solution works excellent for me:

以下解决方案对我来说非常有效:

$monday = strtotime(date('o-\WW'));
$friday = strtotime("next friday",$monday);

For $monday, this method will always return the Monday that started this calendar week. unfortunately, this method relies on PHP 5.1 to parse the odate format.

对于$monday,此方法将始终返回本日历周开始的星期一。不幸的是,这个方法依赖于 PHP 5.1 来解析o日期格式。

To get any day of the week, you could try:

要获得一周中的任何一天,您可以尝试:

function time_for_week_day($day_name, $ref_time=null){
    $monday = strtotime(date('o-\WW',$ref_time));
    if(substr(strtoupper($day_name),0,3) === "MON")
        return $monday;
    else
        return strtotime("next $day_name",$monday);
}

Usage:

用法:

time_for_week_day('wednesday');
time_for_week_day('friday',strtotime('2014-12-25'));