Skip to content

Instantly share code, notes, and snippets.

@gubi
Created September 2, 2014 00:15
Show Gist options
  • Save gubi/4cf77c8da8e93fa8ded0 to your computer and use it in GitHub Desktop.
Save gubi/4cf77c8da8e93fa8ded0 to your computer and use it in GitHub Desktop.
a simple birth-day calculator
<?php
if (isset($_POST["submit"]) && trim($_POST["submit"]) !== "" && isset($_POST["birth"]) && trim($_POST["birth"]) !== "" && preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d\d)$/', $_POST["birth"], $matches)){
$birth = explode("-", $_POST["birth"]);
$giorno = $birth[0];
$mese = $birth[1];
$anno = $birth[2];
$mesi = array (1=>"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
$correggi_num_mese = str_replace("10", "1_", $mese);
$correggi_num_mese = str_replace("0", "", $correggi_num_mese);
$correggi_num_mese = str_replace("1_", "10", $correggi_num_mese);
$nome_mese = $mesi[$correggi_num_mese];
$data_di_nascita = $giorno . " " . $nome_mese . " " . $anno;
$GLOBALS["birthdate"] = $mese . "/" . $giorno . "/" . $anno;
$GLOBALS["birthyear"] = $anno;
$GLOBALS["birthday"] = $giorno;
$data_giusta_di_nascita = $anno . "-" . $mese . "-" . $giorno;
$giorni_di_vita = (int)(abs(strtotime($data_giusta_di_nascita) - strtotime("now"))/86400);
$dec_complegiorno = $giorni_di_vita/100;
if (is_float($dec_complegiorno)){
$prox_complegiorno = ceil($dec_complegiorno)*100;
$diff_giorni = $prox_complegiorno - $giorni_di_vita;
if ($diff_giorni == 1){
$next_complegiorno = "Domani ";
} else if ($diff_giorni > 1 && $diff_giorni <= 10){
if ($diff_giorni == 3){
$next_complegiorno = "Fra " . $diff_giorni . " giorni ";
} else {
$next_complegiorno = "Tra " . $diff_giorni . " giorni ";
}
} else {
$next_date = date("d/m/Y", mktime(0, 0, 0, date("m"), date("d") + $diff_giorni, date("Y")));
list($n_giorno, $n_mese, $n_anno) = explode("/", $next_date);
$g_da_corr = array("01", "02", "03", "04", "05", "06", "07", "08", "09");
$g_corr_con = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
$n_giorno = str_replace($g_da_corr, $g_corr_con, $n_giorno);
if ($n_mese == date("m")){
$next_complegiorno = "Il " . $n_giorno . " ";
} else {
$mesi = array (1=>"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
$correggi_num_mese = str_replace("10", "1_", $n_mese);
$correggi_num_mese = str_replace("0", "", $correggi_num_mese);
$correggi_num_mese = str_replace("1_", "10", $correggi_num_mese);
$nome_n_mese = $mesi[$correggi_num_mese];
$next_complegiorno = "Il " . $n_giorno . " " . $nome_n_mese . " ";
}
}
$next_complegiorno .= "compirai " . $prox_complegiorno . " giorni.";
} else {
$next_complegiorno = "";
}
?>
Oggi hai compiuto <b style="font-size: 1.2em;"><?php print $giorni_di_vita; ?></b> giorni di vita.<br />
<?php print $next_complegiorno; ?>
<?php
} else {
if (isset($_POST["submit"]) && !preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d\d)$/', $_POST["birth"], $matches)) {
print "<span style=\"color: #950000; font-weight: bold;\">La data inserita non &egrave; in un formato valido</span><br /><br />";
}
?>
<form action="" method="post">
<input type="text" value="<?php print $_POST["birth"]; ?>" name="birth" /><br />
Inserire la propria data di nascita in questo formato: <i>gg-mm-aaaa</i><br />
<br />
<input type="submit" value="calcola giorni di vita" name="submit" />
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment