Created
March 8, 2015 17:06
-
-
Save nikkow/b4e754df963efea3c5c3 to your computer and use it in GitHub Desktop.
Calculer un âge en PHP
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 | |
/** | |
* Méthode permettant de calculer l'âge en PHP | |
* à partir d'une date au format JJ/MM/AAAA (à la française) | |
*/ | |
function getAge($dateOfBirth) { | |
// On split la date de naissance au caractère "/" | |
$segments = explode('/', $dateOfBirth); | |
// On calcule le timestamp UNIX correspondant à cette date | |
$timestampDoB = strtotime($segments[2]."-".$segments[1]."-".$segments[0]); | |
// On calcule le nombre de secondes se sont écoulées entre | |
// ce timestamp et maintenant | |
$deltaSec = time() - $timestampDoB; | |
// Combien de secondes y a-t-il dans une année ? Réponse ci-dessous... | |
// (d'après Google...) | |
$secPerYear = 31556926; | |
// On fait un peu de maths... | |
return floor($deltaSec / $secPerYear); | |
} | |
echo "Vu que je suis né le 15 octobre 1989, j'ai ". getAge('15/10/1989') ." ans"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment