Home  >  Article  >  Backend Development  >  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
WBOYOriginal
2016-07-25 08:43:202127browse
  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


Statement:
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
Previous article:php bubble sorting codeNext article:php bubble sorting code