date - PHP: Help on this code for getting all the days in a week of a month -
my code needs weeks along first , last day in month, code working in months, first , last day of last week becomes combination of first day of next month , last day of current month. start of week sunday.
input date in y-m-d format e.g.(2014-01-05)
, the day won't matter because converted first day of month anyway.
this output when month january has problem:
2014-01-01 2014-01-04 2014-01-05 2014-01-11 2014-01-12 2014-01-18 2014-01-19 2014-01-25 2014-01-26 2014-01-31 2014-02-02 2014-01-31 // here lies problem
this output when month february correct:
2014-02-01 2014-02-01 2014-02-02 2014-02-08 2014-02-09 2014-02-15 2014-02-16 2014-02-22 2014-02-23 2014-02-28
the functions i'm using:
function getweeks($from) { $array = array(); $from = date("y-m-d", strtotime($from)); $start_date = date('y-m-01', strtotime($from)); $end_date = date('y-m-t', strtotime($from)); $end_date1 = date('y-m-d', strtotime($end_date." + 6 days")); $week_array = array(); for($date = $start_date; $date < $end_date1; $date = date('y-m-d', strtotime($date. ' + 7 days'))) { $getarray = getweekdates($date, $start_date, $end_date); $week_array[] = $getarray; } return $week_array; } function getweekdates($date, $start_date, $end_date) { $week = date('w', strtotime($date)); $year = date('y', strtotime($date)); $from = date("y-m-d", strtotime("{$year}-w{$week}+3")); if($from < $start_date) $from = $start_date; $to = date("y-m-d", strtotime("{$year}-w{$week}-6")); if($to > $end_date) $to = $end_date; $obj = new stdclass(); $obj->from = $from; $obj->to = $to; return $obj; }
i looked @ get work days in week given date not answer question, because uses different implementation. i'd use own implementation.
on getweeks function:
for($date = $start_date; $date < $end_date1; $date = date('y-m-d', strtotime($date. ' + 7 days'))) { $getarray = getweekdates($date, $start_date, $end_date); if($getarray) $week_array[] = $getarray; // change }
on getweekdates function:
$from = date("y-m-d", strtotime("{$year}-w{$week}+3")); if($from > $end-date) return null; // add if($from < $start_date) $from = $start_date; $to = date("y-m-d", strtotime("{$year}-w{$week}-6"));
Comments
Post a Comment