Date calc - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Date calc.php

  1. <?php
  2. $date1 = "2021-01-01";
  3. $date2 = date('Y-m-d');
  4.  
  5. $diff = abs(strtotime($date2) - strtotime($date1));
  6.  
  7. $years = floor($diff / (365*60*60*24));
  8. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  9. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  10.  
  11. printf("%d years, %d months, %d days\n", $years, $months, $days);
File Description
  • Date calc
  • PHP Code
  • 23 Mar-2021
  • 361 Bytes
You can Share it: