fork download
  1. <?php
  2. $users = [
  3. ['name'=>'John Smith', 'birthday'=>'1988-02-03'],
  4. ['name'=>'Jane Jones', 'birthday'=>'2014-07-08']
  5. ];
  6. // Calculate and store the age in years of each user
  7. foreach($users as &$user) {
  8. $today = new DateTime();
  9. $birthday = new DateTime($user['birthday']);
  10. $age = $today->diff($birthday);
  11. $user['age'] = $age->format("%y");
  12. }
  13. unset($user);
  14. ?>
  15.  
  16. $users = [
  17. ['name'=>'John Smith', 'birthday'=>'1988-02-03'],
  18. ['name'=>'Jane Jones', 'birthday'=>'2014-07-08']
  19. ];
  20.  
  21. foreach($users as $index => $user) {
  22. $users[$index]['age'] = calculateAge($user['birthday'])->format("%y");
  23. }
  24.  
  25. var_dump($users);
  26.  
  27. function calculateAge($date) {
  28. return date_diff(date('Y-m-d'), date_create($date));
  29. }
  30.  
  31. /*EOF*/
Runtime error #stdin #stdout #stderr 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Fatal error:  DateTime::__construct(): Timezone database is corrupt - this should *never* happen! in /home/JGWBRZ/prog.php on line 8