2009-09-16 58 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

+0

週末として。 – Psyche

答えて

52

これらのstrtotime入力は非常にうまく機能:

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

すべてを行う必要が文の場合、いくつかの内部のロジックをラップすることです。

+1

素敵なAnsしかし、私は日付からmondayを取得したいですか? 2012年1月1日が@ 01-2012を返します。 – chhameed

+7

@chhameed: 'strtotime(" next monday "、strtotime(" 2012-01-01 "))'。 2番目のパラメータが指定されている場合は、参照日付として使用されます。 –

+1

おはようありがとう...本当に助けてくれた..... – chhameed

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

をこの質問は、日付と時刻の答えを必要とします: -

/** 
* @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については
$monday = strtotime(date('o-\WW')); 
$friday = strtotime("next friday",$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

私は今週だけでなく、月曜日も欲しかった以外は、同様のものを探していました。これは私が思いついたものです:

これは、任意の日付を受け入れ、日曜日を与えます。その後、月曜日から土曜日までの日数を簡単に追加することができます。

33

最善の解決策は、次のようになります。

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

これは素晴らしいです。ありがとう。仕事は短くて可読ですか –

+0

これは受け入れられた答えよりもはるかに優れています。 「この金曜日」と「次の金曜日」には違いはありません。あなたは「今週の金曜日」と「来週の金曜日」を使用しなければなりません。 – DrDamnit

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'))); 
関連する問題