1
$today= new DateTime(date('Y-m-d'));
$dob= new DateTime($user_dob);
$diff = $today->diff($dob);
return $diff->y .' Year';

I am trying to calculate age but its showing error Call to undefined method DateTime::diff() how can i solve this problem? i m using PHP Version 5.4.24

Niroj Adhikary
  • 1,775
  • 18
  • 30

2 Answers2

0

internal objects in php5, on failure, can return null in the __construct. You can check for:

if (null !== $today) {
    $diff = $today->diff($dob);
    return $diff->y .' Year';
}

The value that is being returned by date('Y-m-d') would also help understanding why the method is failing

Bolovsky
  • 538
  • 7
  • 12
0

In the answers there is a good option with calculating the days. But for correct calculation, use "floor" instead of "round"

$start = new DateTime('2020-06-20');
$end = new DateTime();
$days = floor(($end->format('U') - $start->format('U')) / (60*60*24));

The system does not allow me to leave a comment