Created
November 6, 2014 11:55
-
-
Save A35G/4697b9e76ffa98903269 to your computer and use it in GitHub Desktop.
Through a date given as input, it returns the same date or the first date to make a payment by a deadline. A check is performed on the specified date, to see if the same appears as Italian festivity.
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 | |
// Check if var is a valid date | |
function validateDate($date, $format = 'Y-m-d H:i:s') { | |
$d = DateTime::createFromFormat($format, $date); | |
return $d && $d->format($format) == $date; | |
} | |
/** | |
* Check day of week for specified date | |
* ----------------------------------------------------------- | |
* N is ISO-8601 numeric representation of the day of the week | |
* 1 (for Monday) through 7 (for Sunday) | |
* ----------------------------------------------------------- | |
*/ | |
function checkWeek($check_data) { | |
$res_d = ""; | |
$check_d = date('N', strtotime($check_data)); | |
$prtd = explode("-", $check_data); | |
if ($check_d == 6) // Saturday | |
$res_d = date("Y-m-d", mktime(0,0,0,$prtd[1],($prtd[2]+2),$prtd[0])); | |
elseif ($check_d == 7) // Sunday | |
$res_d = date("Y-m-d", mktime(0,0,0,$prtd[1],($prtd[2]+1),$prtd[0])); | |
else | |
$res_d = $check_data; | |
return $res_d; | |
} | |
// Return label with utils date | |
function parseDate($good_day) { | |
// Label for week days | |
$week_days = array('','Lunedì','Martedì','Mercoledì','Giovedì','Venerdì','Sabato','Domenica'); | |
// Label for month | |
$month_lab = array('', 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'); | |
$wk_d = date('N', strtotime($good_day)); | |
$pzd = explode("-", $good_day); | |
$data_parsed = $week_days[$wk_d].", ".$pzd[2]." ".$month_lab[intval($pzd[1])]." ".$pzd[0]; | |
return sprintf("Data utile: %s", $data_parsed); | |
} | |
function getPayDay($day_deadline='') { | |
$bl_data = ""; | |
// Only for Italy - Fix date | |
$festivity = array( | |
"01-01", // Capodanno | |
"01-06", // Epifania | |
"04-25", // Liberazione dal nazifascismo (1945) | |
"05-01", // Festa del lavoro | |
"06-02", // Festa della Repubblica | |
"08-15", // Assunzione di Maria | |
"11-01", // Ognissanti | |
"12-08", // Immacolata Concezione | |
"12-25", // Natale di Gesu' | |
"12-26" // Santo Stefano | |
); | |
// Check if var isn't empty and if a valid date | |
if (isset($day_deadline) && !empty($day_deadline) && validateDate($day_deadline, 'Y-m-d')) { | |
// Check day of week for specified date | |
$bl_data = checkWeek($day_deadline); | |
$przd = explode("-", $bl_data); | |
// Calculate Easter Day | |
$east_d = easter_days($przd[0]); | |
$easter_day = date("Y-m-d", mktime(0,0,0,"03",("21"+$east_d),$przd[0])); | |
// Calculate Next Monday | |
// Lunedi' dell'Angelo - Pasquetta | |
$east_d_m = easter_days($przd[0])+1; | |
$monday_easter_day = date("Y-m-d", mktime(0,0,0,"03",("21"+$east_d_m),$przd[0])); | |
// If exists list of festivity, add Easter day and next Monday of Easter | |
if (isset($festivity) && !empty($festivity) && is_array($festivity)) { | |
array_push($festivity, substr($easter_day, 5), substr($monday_easter_day, 5)); | |
// Check if date declared, is a festivity | |
if (in_array(substr($bl_data, 5), $festivity) !== false) | |
$bl_data = date("Y-m-d", mktime(0,0,0,$przd[1],($przd[2]+1),$przd[0])); | |
} else { | |
if ($bl_data == $easter_day) | |
$bl_data = date("Y-m-d", mktime(0,0,0,$przd[1],($przd[2]+2),$przd[0])); | |
elseif ($bl_data == $monday_easter_day) | |
$bl_data = date("Y-m-d", mktime(0,0,0,$przd[1],($przd[2]+1),$przd[0])); | |
} | |
// Check again day of week for date | |
$bl_data = checkWeek($bl_data); | |
} | |
return (!empty($bl_data)) ? parseDate($bl_data) : "Error!"; | |
} | |
// Init Script | |
echo getPayDay("2015-05-01"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment