Home  >  Article  >  Backend Development  >  Check if the date is a legal date

Check if the date is a legal date

WBOY
WBOYOriginal
2016-07-25 09:10:121074browse
Check if the date is a legal date
  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])) { //Open source OSPhP.COM.CN
  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. //OSPHP.COM.Cn open source
  10. $timeArr = explode(":", $time);
  11. if (is_numeric( $timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
  12. //Open source code OSPHP.COM.Cn
  13. if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] < ;= 59))
  14. //PHP open source code
  15. return true;
  16. else
  17. return false;
  18. }
  19. return false;
  20. }
  21. function DateDiff($date1, $date2, $unit = "") { //Time comparison Function, returns the difference in seconds, minutes, hours or days between two dates
  22. switch ($unit) {
  23. case 's':
  24. $dividend = 1;
  25. break;
  26. case 'i':
  27. $dividend = 60;
  28. //oSPHP.COM.CN
  29. break;
  30. case 'h':
  31. $dividend = 3600;
  32. break;
  33. case 'd':
  34. $dividend = 86400;
  35. break;
  36. default:
  37. $dividend = 86400;
  38. }
  39. $time1 = strtotime($date1);
  40. $time2 = strtotime($date2);
  41. if ($time1 && $time2) //OSPHP.com.CN
  42. return (float)($time1 - $ time2) / $dividend;
  43. return false;
  44. }
  45. ?>
Copy code


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