2009-09-16 5 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하지만 나는 월요일부터 월요일에 오기를 원합니다. 2012 년 1 월 1 일을 반환합니다. 2-01-2012 – chhameed

+7

@chhameed :'strtotime ("next monday", strtotime ("2012-01-01"))'. 두 번째 매개 변수가 지정되면 참조 날짜로 사용됩니다. –

+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. "일요일 ": 나는 병이 오늘 날짜
  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 = 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

나는 이번 주뿐만 아니라 월요일을 원했던 것을 제외하고는 비슷한 것을 찾고있었습니다. 이것은 내가 생각해 낸 것입니다 :

그것은 임의의 날짜를 받아들이고 일요일을줍니다. 그런 다음 월요일부터 토요일까지 다시 일을 쉽게 추가 할 수 있습니다.

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'))); 
관련 문제