2016-05-14 15 views
5

Tôi muốn chia tháng hiện tại thành các tuần như ngày đầu tiên đến thứ bảy và sau đó chủ nhật đến thứ bảy tới. Ví dụ: tháng 5, Vì vậy, tôi muốn chia nhỏ nhưTách tháng hiện tại theo tuần trong php

2016-05-01 to 2016-05-07 
2016-05-08 to 2016-05-14 
2016-05-15 to 2016-05-21 
2016-05-22 to 2016-05-28 
2016-05-29 to 2016-05-31 

nếu tôi thử mã bên dưới, tôi không nhận được kết quả chính xác.

<?php 

$start_date = date('Y-m-d', strtotime('2016-06-01')); 
$end_date = date('Y-m-d', strtotime('2016-06-30')); 
$i=1; 
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d',  strtotime($date. ' + 7 days'))) { 
    echo getWeekDates($date, $start_date, $end_date, $i); 
    echo "\n"; 
    $i++; 
} 

function getWeekDates($date, $start_date, $end_date, $i) { 
    $week = date('W', strtotime($date)); 
    $year = date('Y', strtotime($date)); 

    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); 
    if($from < $start_date) $from = $start_date; 

    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7")); 
    if($to > $end_date) $to = $end_date; 

    echo $from." - ".$to.'<br>';  
} 

?> 

tôi đã nhận như

2016-05-01 - 2016-05-01 
2016-05-01 - 2016-05-08 
2016-05-08 - 2016-05-15 
2016-05-15 - 2016-05-22 
2016-05-22 - 2016-05-29 

Trong vòng lặp for, trong khi kiểm tra thiết lập cuối cùng của ngày, conditon nhận thất bại .so nó không tính toán 30,31.

Tôi có thể làm điều đó bằng cách nào?

+0

có thể đây là vấn đề W - Tôi Số tuần SO-8601 trong tuần (tuần bắt đầu từ thứ Hai) – JYoThI

Trả lời

1

Ngày giờ, DatePeriod và DateInterval là bạn bè của bạn để thực hiện điều đó một cách nhanh chóng!

See online.

<?php 
/** 
* @file 
* Awnser to http://stackoverflow.com/q/37224961/392725 
* 
* @link http://stackoverflow.com/a/37228497/392725 
*/ 
date_default_timezone_set('Europe/Paris'); 

function getWeekDates(DateTimeInterface $date, $format = 'Y-m-d') { 
    $dt  = \DateTimeImmutable::createFromMutable($date); 
    $first_day = $dt->modify('first day of this month'); 
    $last_day = $dt->modify('last day of this month'); 
    $period = new \DatePeriod(
    $first_day, 
    \DateInterval::createFromDateString('sunday this week'), 
    $last_day, 
    \DatePeriod::EXCLUDE_START_DATE 
); 

    $weeks = [$first_day->format($format)]; 
    foreach ($period as $d) { 
    $weeks[] = $d->modify('-1 day')->format($format); 
    $weeks[] = $d->format($format); 
    } 
    $weeks[] = $last_day->format($format); 

    return array_chunk($weeks, 2); 
} 

echo "Weeks of the current month:\n"; 
$weeks = getWeekDates(new \DateTime('now')); 
array_walk($weeks, function ($week) { 
    vprintf("%s to %s\n", $week); 
}); 

echo "\nWeeks of the month may 2016:\n"; 
$weeks = getWeekDates(new \DateTime('2016-05')); 
array_walk($weeks, function ($week) { 
    vprintf("%s to %s\n", $week); 
}); 

echo "\nWeeks of the month june 2016:\n"; 
$weeks = getWeekDates(new \DateTime('2016-06')); 
array_walk($weeks, function ($week) { 
    vprintf("%s to %s\n", $week); 
}); 

echo "\nWeeks of the month october 2016:\n"; 
$weeks = getWeekDates(new \DateTime('2016-10')); 
array_walk($weeks, function ($week) { 
    vprintf("%s to %s\n", $week); 
}); 

Kết quả:

Weeks of the current month: 
2016-05-01 to 2016-05-07 
2016-05-08 to 2016-05-14 
2016-05-15 to 2016-05-21 
2016-05-22 to 2016-05-28 
2016-05-29 to 2016-05-31 

Weeks of the month may 2016: 
2016-05-01 to 2016-05-07 
2016-05-08 to 2016-05-14 
2016-05-15 to 2016-05-21 
2016-05-22 to 2016-05-28 
2016-05-29 to 2016-05-31 

Weeks of the month june 2016: 
2016-06-01 to 2016-06-04 
2016-06-05 to 2016-06-11 
2016-06-12 to 2016-06-18 
2016-06-19 to 2016-06-25 
2016-06-26 to 2016-06-30 

Weeks of the month october 2016: 
2016-10-01 to 2016-10-01 
2016-10-02 to 2016-10-08 
2016-10-09 to 2016-10-15 
2016-10-16 to 2016-10-22 
2016-10-23 to 2016-10-29 
2016-10-30 to 2016-10-31 
2

Hãy thử như thế này, tôi giả sử bạn có thể thay đổi biến tháng theo nhu cầu của bạn.

$month=5;//pass here your month 
$first_date = date("Y-m-1"); 
do{ 
    $last_date = date("Y-m-d",strtotime($first_date. " +6 days")); 
    $month = date("m",strtotime($last_date)); 
    if($month!=5) 
    $last_date = date("Y-m-t"); 

    echo "<br>".$first_date." - ".$last_date; 
    $first_date = date("Y-m-d",strtotime($last_date. " +1 days")); 
}while($month == 5); 

Kiểm tra ở đây: https://eval.in/571521

0
function getSaturdays($y, $m) 
{ 
    return new DatePeriod(
     new DateTime("first saturday of $y-$m"), 
     DateInterval::createFromDateString('next saturday'), 
     new DateTime("last day of $y-$m") 
    ); 
} 


function list_week_days($year, $month) { 
    $first_month_day = new DateTime("first day of $year-$month") ; 
    echo $first_month_day->format("Y-m-d\n") . '-'; 
    foreach (getSaturdays($year, $month) as $saturday) { 
     echo $saturday->format(" Y-m-d\n"); 
     echo '-<br>'; 
     $sunday = $saturday->modify('next Sunday'); 
     echo $sunday->format(" Y-m-d\n"); 

    } 
    $last_month_day = new DateTime("last day of $year-$month"); 
    echo $last_month_day->format("Y-m-d\n"); 
} 


list_week_days(2016, 5); 

tái bút: Tôi khuyến khích bạn sử dụng lớp DateTime

0

đổi mã Rishi của, cố định một số nơi vấn đề và giải quyết vấn đề của năm nhuận khi tách Tháng hai vào tuần:

$weeks = monthToWeeks(2016, '02'); 

print_r($weeks); 

function monthToWeeks($y, $m) 
{ 
    $weeks = []; 
    $month = $m; 
    $first_date = date("{$y}-{$m}-01"); 

    do { 
     $last_date = date("Y-m-d", strtotime($first_date. " +6 days")); 
     $month = date("m", strtotime($last_date)); 

     if ($month != $m) { 
      $last_date = date("Y-m-t", mktime(0, 0, 0, $m, 1, $y)); 

      if ($first_date > $last_date) { 
       break; 
      } 
     } 

     $weeks[] = [$first_date, $last_date]; 

     $first_date = date("Y-m-d", strtotime($last_date. " +1 days")); 

    } while($month == intval($m)); 

    return $weeks;  
} 

Bạn có thể thử nó tại đây https://eval.in/643724

Các vấn đề liên quan