Last active
September 6, 2019 23:13
-
-
Save samarthagarwal/706e4f95206fa865203ccb4a9e6a8e7e to your computer and use it in GitHub Desktop.
A simple task for a secret operation ;)
This file contains hidden or 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 | |
$date = date('d-m-Y'); | |
$currentMonth = explode("-", $date)[1]; | |
$currentYear = explode("-", $date)[2]; | |
$response = array(); | |
for ($i = $currentMonth; $i <= 12; $i++) { | |
// Getting name of the month from i | |
$dateObj = DateTime::createFromFormat('!m', $i); | |
$monthName = $dateObj->format('F'); | |
echo $monthName . "\t"; | |
// Getting the date of the last working day in the month | |
$lastWorkingdate = getLastWorkingDayByMonth($i, $currentYear); | |
echo getLastWorkingDayByMonth($i, $currentYear) . "\t"; | |
$bonusDate = getBonusDayByMonth($i, $currentYear); | |
echo $bonusDate . "\n"; | |
$json = new \stdClass(); // Don't know why this is required | |
$json->Month = $monthName; | |
$json->Salaries_Payment_Day = $lastWorkingdate; | |
$json->Bonus_Payment_Day = $bonusDate; | |
array_push($response, $json); | |
} | |
echo "\n"; | |
echo json_encode($response); | |
// The following function takes the numner (9) and year (2019) as arguments and returns the date of the last working day of the month. | |
// Saturday and Sunday are not conidered as the working days | |
function getLastWorkingDayByMonth($month, $year) { | |
$numberOfDaysInCurrentMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); | |
$unixTimestamp = strtotime($numberOfDaysInCurrentMonth . '-' . $month . '-' . $year); | |
$dayOfWeek = date("l", $unixTimestamp); | |
if($dayOfWeek == "Saturday") { | |
return $numberOfDaysInCurrentMonth - 1; | |
} else if($dayOfWeek == "Sunday") { | |
return $numberOfDaysInCurrentMonth - 2; | |
} else { | |
return $numberOfDaysInCurrentMonth; | |
} | |
} | |
function getBonusDayByMonth($month, $year) { | |
$numberOfDaysInCurrentMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); | |
$unixTimestamp = strtotime($numberOfDaysInCurrentMonth . '-' . $month . '-' . $year . " +15 days"); | |
$dayOfWeek = date("l", $unixTimestamp); | |
if($dayOfWeek == "Saturday" || $dayOfWeek == "Sunday") { | |
// Finding first thursday after 15th | |
for ($i = 1; $i < 7; $i++) { | |
$unixTimestamp = strtotime($numberOfDaysInCurrentMonth . '-' . $month . '-' . $year . " +" . (15 + $i)." days"); | |
$dayOfWeek = date("l", $unixTimestamp); | |
if($dayOfWeek == "Thursday") { | |
return $i + 15; | |
} | |
} | |
} else { | |
return 15; | |
} | |
echo $dayOfWeek; | |
} | |
?> | |
/* | |
September 30 15 | |
October 31 15 | |
November 29 19 | |
December 31 15 | |
[ | |
{ | |
"Month":"September", | |
"Salaries_Payment_Day":30, | |
"Bonus_Payment_Day":15 | |
},{ | |
"Month":"October", | |
"Salaries_Payment_Day":31, | |
"Bonus_Payment_Day":15 | |
},{ | |
"Month":"November", | |
"Salaries_Payment_Day":29, | |
"Bonus_Payment_Day":19 | |
},{ | |
"Month":"December", | |
"Salaries_Payment_Day":31, | |
"Bonus_Payment_Day":15 | |
} | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment