Skip to content

Instantly share code, notes, and snippets.

@ayhanbaris
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayhanbaris/43afd5deefc08244e824 to your computer and use it in GitHub Desktop.
Save ayhanbaris/43afd5deefc08244e824 to your computer and use it in GitHub Desktop.
verilen bir yıl-ay için , o ay'ın haftalarının başlangic-bitiş tariherini döndüren bir php fonksiyonu
/**
* verilen bir yıl-ay için , o ay'ın haftalarının başlangic-bitiş tariherini döndüren bir php fonksiyonu
* not 1: biraz uzuncana bir fonksiyon oldu ama idare edin :)
* not 2: haftanın son gunu "pazar" kabul edildi.
*/
function explodeMonthToWeeks($year, $month) {
$month = str_pad($month,2,'0',STR_PAD_LEFT);
$firstDayOfMonth = $year.'-'.$month.'-01';
$firstSunday = new DateTime('first sunday of ' . $firstDayOfMonth );
$lastDayOfMonth = new DateTime($firstDayOfMonth);
$lastDayOfMonth = $lastDayOfMonth->modify(
sprintf('+%d days', $lastDayOfMonth->format('t') - $lastDayOfMonth->format('j'))
);
$begin = new DateTime( $firstSunday->format( "Y-m-d" ) );
$end = new DateTime( $lastDayOfMonth->format( "Y-m-d" ) );
$interval = DateInterval::createFromDateString('1 week');
$period = new DatePeriod($begin, $interval, $end);
$ii = 0;
foreach ( $period as $dt ) {
$ii++;
if ($ii==1) {
# first week
$date[$ii] = array($firstDayOfMonth, $dt->format( "Y-m-d" ));
}
else {
#mid weeks
$tmp = clone $dt;
$tmp->modify(
sprintf('-%d days', 6)
);
$date[$ii] = array( $tmp->format( "Y-m-d" ) , $dt->format( "Y-m-d" ));
}
}
#last week
$ii++;
$tmp = clone $dt;
$tmp->modify(
sprintf('+%d days', 1)
);
$date[$ii] = array( $tmp->format( "Y-m-d" ) , $lastDayOfMonth->format( "Y-m-d" ));
return $date;
}
# Örnek:
print_r( explodeMonthToWeeks(2014,2) );
# Çıktı:
/**
Array
(
[1] => Array
(
[0] => 2014-02-01
[1] => 2014-02-02
)
[2] => Array
(
[0] => 2014-02-03
[1] => 2014-02-09
)
[3] => Array
(
[0] => 2014-02-10
[1] => 2014-02-16
)
[4] => Array
(
[0] => 2014-02-17
[1] => 2014-02-23
)
[5] => Array
(
[0] => 2014-02-24
[1] => 2014-02-28
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment