Home >Backend Development >PHP Tutorial >Summary of commonly used PHP date functions

Summary of commonly used PHP date functions

WBOY
WBOYOriginal
2016-07-25 08:45:09889browse

Summary of commonly used date functions in PHP: including checking whether the date format is legal, checking whether the time is legal, time comparison functions, and returning seconds, minutes, hours or days between two dates.

  1. function check_date($date) { //Check whether the date is a legal date
  2. $dateArr = explode("-", $date);
  3. if (is_numeric($dateArr[0]) && is_numeric ($dateArr[1]) && is_numeric($dateArr[2])) {
  4. return checkdate($dateArr[1],$dateArr[2],$dateArr[0]);
  5. }
  6. return false;
  7. }
  8. function check_time($time) { //Check whether the time is legal time
  9. $timeArr = explode(":", $time);
  10. if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric ($timeArr[2])) {
  11. if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
  12. return true;
  13. else
  14. return false;
  15. }
  16. return false;
  17. }
  18. function DateDiff ($date1, $date2, $unit = "") { //Time comparison function, returns the seconds, minutes, hours or days between two dates
  19. switch ($unit) {
  20. case 's':
  21. $dividend = 1;
  22. break;
  23. case 'i':
  24. $dividend = 60;
  25. break;
  26. case 'h':
  27. $dividend = 3600;
  28. break;
  29. case 'd':
  30. $dividend = 86400;
  31. break;
  32. default:
  33. $dividend = 86400;
  34. }
  35. $time1 = strtotime($date1);
  36. $time2 = strtotime($date2);
  37. if ($time1 && $time2) //OSPHP.COM.Cn Open source
  38. return (float)($time1 - $time2) / $dividend;
  39. return false;
  40. }
  41. ?>
Copy code

php


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