Last active
December 19, 2015 15:48
-
-
Save filaruina/5978619 to your computer and use it in GitHub Desktop.
A simple function to find work days in a time period, using DateTime, DateInterval and DatePeriod.
Some refactoring tips are appreciated =)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function findWorkDays($dateStart, $dateEnd) | |
{ | |
$oneWeek = new \DateInterval('P7D'); | |
$date1 = new \DateTime($dateStart); | |
$date2 = new \DateTime($dateEnd); | |
$interval = $date1->diff($date2); | |
$freeDays = 0; | |
//Finding saturdays | |
$satObj = clone $date1; | |
$saturdays = new \DatePeriod($satObj->modify('saturday'), $oneWeek, $date2); | |
foreach ($saturdays as $saturday) { | |
$freeDays++; | |
} | |
//Finding sundays | |
$sunObj = clone $date1; | |
$sundays = new \DatePeriod($sunObj->modify('sunday'), $oneWeek, $date2); | |
foreach ($sundays as $sunday) { | |
$freeDays++; | |
} | |
return $interval->days - $freeDays + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently it doesn't consider holydays