Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
<?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&igrave;','Marted&igrave;','Mercoled&igrave;','Gioved&igrave;','Venerd&igrave;','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