×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Nightcry ncy
Added: Apr 30, 2021 2:02 PM
Modified: Apr 30, 2021 2:02 PM
Views: 3989
Tags: no tags
  1. public static function calcAge($dob, $toDate = null) {
  2.         $dt = new DateTime();
  3.        
  4.         // if $toDate was not specified, use TODAY date
  5.         $toDate = $toDate ?? $dt->format('Y-m-d');
  6.  
  7.         // Get difference between $toDate and $dob
  8.         $interval = date_diff(date_create($toDate), date_create($dob));
  9.  
  10.         return $interval->format("%Y-%M-%d");
  11.     }