나와 함께 사용할 수있는 날짜가 있다고 가정합니다. 2016-07
다음과 같이 주 날짜 범위를 찾고 싶습니다.
2016-07-03 to 2016-07-09
2016-07-10 to 2016-07-16
2016-07-17 to 2016-07-23
2016-07-24 to 2016-07-30
어떻게해야합니까? 나는 사용해 MySql
보았지만 실패했습니다. 이제하려고합니다 php
.
이 코드는 원하는대로 정확히 수행해야합니다. 그렇지 않은 경우 또는 질문이있는 경우
$date = "2016-07"; //Your date, can be any given date of this format
$monthAndYear = explode("-", $date);
$weeks = array();
$start = mktime(0, 0, 0, $monthAndYear[1], 1, $monthAndYear[0]);
$start -= 86400; //There are surely better ways
//find beginning if first week
do
{
$start += 86400;// Add one day
$tmp = date("N", $start);
}
while($tmp != 1);
//Get all weeks. If you want only those which are in the current month use this instead:
//while(date("n", ($start + 6 * 86400)) == $monthAndYear[1])
while(date("n", $start) == $monthAndYear[1])
{
$date = date("Y-m-d", $start);
$start += (6 * 86400);
$date .= " to ".date("Y-m-d", $start);
$weeks[] = $date;
$start += 86400;
}
echo "<pre>";
var_dump($weeks);
echo "</pre>";
출력은 다음과 같습니다.
array(4) {
[0]=>
string(24) "2016-07-04 to 2016-07-10"
[1]=>
string(24) "2016-07-11 to 2016-07-17"
[2]=>
string(24) "2016-07-18 to 2016-07-24"
[3]=>
string(24) "2016-07-25 to 2016-07-31"
}
두 번째 while
옵션을 사용하면 31 일이 월말이자 한주의 끝이기 때문에 결과는 동일합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다