Home > Backend Development > PHP Tutorial > PHP time comparison function, returns the seconds, minutes, hours or days between two dates

PHP time comparison function, returns the seconds, minutes, hours or days between two dates

WBOY
Release: 2016-07-25 08:43:20
Original
2223 people have browsed it
  1. function DateDiff($date1, $date2, $unit = "") { //Time comparison function, returns the number of seconds, minutes, hours or days between two dates
  2. switch ($unit) {
  3. case 's':
  4. $dividend = 1;
  5. break;
  6. case 'i':
  7. $dividend = 60; //oSPHP.COM.CN
  8. break;
  9. case 'h':
  10. $dividend = 3600;
  11. break ;
  12. case 'd':
  13. $dividend = 86400;
  14. break; //Open source OSPhP.COM.CN
  15. default:
  16. $dividend = 86400;
  17. }
  18. $time1 = strtotime($date1);
  19. $time2 = strtotime ($date2);
  20. if ($time1 && $time2)
  21. return (float)($time1 - $time2) / $dividend;
  22. return false;
  23. }
Copy code

Days, minutes, seconds


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template