Last active
February 28, 2016 12:10
-
-
Save abedsujan/eda18520bec975bd7324 to your computer and use it in GitHub Desktop.
PHP get age year from birthdate
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 | |
function birthday($birthday){ | |
$age = strtotime($birthday); | |
if($age === false){ | |
return false; | |
} | |
list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); | |
$now = strtotime("now"); | |
list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); | |
$age = $y2 - $y1; | |
if((int)($m2.$d2) < (int)($m1.$d1)) | |
$age -= 1; | |
return $age; | |
} | |
echo birthday('1981-05-18'); | |
// From PHP 5.3+ | |
function birthday($birthday) { | |
$age = date_create($birthday)->diff(date_create('today'))->y; | |
return $age; | |
} | |
echo birthday('1981-05-18'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment