2009-09-16 277 views
19

如何獲得當週的週一和週五的日期?獲取本週的週一和週五的日期(PHP)

我有以下代碼,但如果當前日期是星期日或星期六,則代碼失敗。

$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")); 
+2

你計數週日開始或週末? – Greg 2009-09-16 08:21:05

+0

截止本週末。 – Psyche 2009-09-16 08:56:37

回答

52

這些投入的strtotime很好地工作:

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

所有你需要做的是包裝裏面的一些if語句的邏輯。

+1

不錯的答案,但我想從日期星期一得到什麼。如01-01-2012將返回2-01-2012 – chhameed 2012-11-02 10:33:31

+7

@chhameed:'strtotime(「下一個星期一」,strtotime(「2012-01-01」))''。第二個參數(如果指定)用作參考日期。 – 2012-11-02 11:58:49

+1

哦,非常感謝......它真的幫了我..... – chhameed 2012-11-03 05:50:58

1

這真的取決於你如何定義一週但我想出了這個功能會給你最近的「星期一」或「星期五」的日期(或與此有關的任何一天):

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

} 

輸入:一週中的某一天(「星期日」,「星期一」,等等)

輸出:如果我問最近的「星期日」,今天是:

  1. 「星期日「:我w生病得到今天的日期
  2. 「星期一」:我將獲得昨天的日期
  3. 「星期六:我會得到明天的日期

希望這有助於:)

9

這個問題需要一個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; 
} 

用法:

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

輸出:

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

See it working

0

我所需要的電流每週ISO 8601我想星期一總是被定義爲這個電流本週開始了週一的定義。

以下解決方案對我的作品優秀:

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

$monday,此方法將總是返回開始這個日曆周的星期一。不幸的是,這種方法依賴於PHP 5.1來解析日期格式o

要獲得每週的任何一天,你可以嘗試:

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

用法:

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

由於頂端回答表明,使用PHP的strtotime()功能是最簡單的方法。

但是,不是像他所建議的那樣使用if語句,而是簡單地重置回前一個星期日,並從那裏獲取所需的日期。

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

我在找一個類似的東西,除了我想要任何一個星期一,不只是這個星期。這是我想出了:

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

它接受任意日期,並給星期天。然後,您可以輕鬆添加週日到週六的日子。

33

最好的解決辦法是:

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

這太棒了。謝謝。這份工作是否簡短易讀 – 2016-06-30 13:40:36

+0

這比接受的答案要好得多。 「這個星期五」和「下一個星期五」沒有區別。你必須使用「本週星期五」和「星期五下週」 – DrDamnit 2017-10-18 20:24:55

0

我使用:

$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'))); 
相關問題