0

I want to count the days in a date, the sample code that I use as below

<?php
  $birthDate = "12-8-2018"; ( m-d-Y)
  $birthDate = explode("-", $birthDate);
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is:" . $age; // OUTPUT is -1
?>

the results I get are only in the form of years, how do I get results in the form of the number of days, an example for the results in the code above should be 19

  • 1
    Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Muhammad Usman Aug 30 '18 at 23:16

1 Answers1

0

The solution is simple:

<?php
$birthDate = "12-8-2018";                   // Input date
$birthDate = strtotime($birthDate);         // Convert to unix time

$days = (time() - $birthDate)/(60*60*24);   // difference / (60 seconds*60 minutes*24 hours)
?>

$days will be float use floor() function to convert it to integer value:

echo floor($days);  // 18.674826388889 -> 18

View php fiddle

TheMisir
  • 4,083
  • 1
  • 27
  • 37