Home  >  Article  >  Backend Development  >  php时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天

php时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天

WBOY
WBOYOriginal
2016-07-25 08:43:202050browse
  1. function DateDiff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
  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; //开源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. }
复制代码

几天, 几分钟, 几秒


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