Home > Backend Development > PHP Tutorial > PHP calculates the number of days difference between dates (the time difference between any time and the current time)

PHP calculates the number of days difference between dates (the time difference between any time and the current time)

WBOY
Release: 2016-07-25 08:51:36
Original
3215 people have browsed it
  1. function count_days($a,$b){
  2. $a_dt=getdate($a);
  3. $b_dt=getdate($b);
  4. $a_new=mktime(12,0, 0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']);
  5. $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt[ 'mday'],$b_dt['year']);
  6. return round(abs($a_new-$b_new)/86400);
  7. }
  8. //How many days are there between today and October 11, 2008
  9. $date1= strtotime(time());
  10. $date1=strtotime('10/11/2008');
  11. $result=count_days($date1,$date2);
  12. echo $result;
  13. ?>
Copy code

Method 2:

  1. //How many days are there between today and September 9, 2008
  2. $Date_1=date("Y-m-d");
  3. $Date_2="2008-10-11";
  4. $d1 =strtotime($Date_1);
  5. $d2=strtotime($Date_2);
  6. $Days=round(($d2-$d1)/3600/24);
  7. echo "The difference between today and October 11, 2008". $Days."Days";
  8. ?>
Copy code

PHP Get the time difference between any time and the current time

Code:

  1. #Function: Get the time difference between any time and the current time
  2. function QueryDays($datestr){
  3. #Format time
  4. $da=preg_split("/(-| |:)/i ",$datestr);
  5. $nowyear=date("Y");
  6. $nowmon=date("n");
  7. $nowday=date("d");
  8. $nowtimes=mktime(0,0,0 ,$nowmon,$nowday,$nowyear);
  9. $pdtimes= mktime(0,0,0,$nowmon,$nowday,$nowyear-1);
  10. $bjtimes= mktime(0,0,0,$da[ 1],$da[2],$da[0]);
  11. #Judge whether the time given is within one year
  12. if ($bjtimes>=$pdtimes and $bjtimes<=$nowtimes){
  13. return ( floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[ 1],$da[2],$da[0]))));
  14. }else{
  15. $loop=$nowyear-$da[0];
  16. $totaldays=(floor(strftime("%j", mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
  17. for($i=1;$i<=$ loop;$i++){
  18. for($j=12;$j>=1;$j--){
  19. if ($da[0]==$nowyear-$i and $da[1]==$ j){
  20. $days=MonDays($nowyear-$i,$j);
  21. return $totaldays+=$days-$da[2];
  22. break;
  23. }else{
  24. $days=MonDays($nowyear-$ i,$j);
  25. $totaldays+=$days;
  26. }//end else
  27. }//end for
  28. }//end for
  29. }//end else
  30. }//end function
  31. #Get the number of days in the month
  32. function MonDays($year,$month){
  33. switch ($month){
  34. case "1":
  35. case "3":
  36. case "5":
  37. case "7":
  38. case "8":
  39. case "10":
  40. case "12": $days=31;break;
  41. case "4":
  42. case "6":
  43. case "9":
  44. case "11": $days=30;break;
  45. case "2":
  46. if (checkdate($month,29,$year)){
  47. $days=29;
  48. }else{
  49. $days=28;
  50. }//end else
  51. break;
  52. }//end switch
  53. return $days;
  54. }//end function
  55. $datestr="2002-1-14 9:47:20";
  56. echo QueryDays($datestr);
  57. ?>
Copy code

PHP Calculate the year, month, hour, minute and second difference between two times

How to calculate the difference between two times in years, months, hours, minutes and seconds in PHP

First convert the two times into timestamps, then subtract them to get the seconds difference between the two times, and finally do some arithmetic to get the year, month, day, hour, minute and second difference between the two times.

Code:

  1. $time1 = "2008-6-15 11:49:59"; //The first time
  2. $time2 = "2007-5-5 12:53:28"; //The second time
  3. $t1 = strtotime($time1);
  4. $t2 = strtotime($time2);
  5. $t12 = abs($t1-$t2);
  6. $start = 0;
  7. $string = " The difference between the two times: ";
  8. $y = floor($t12/(3600*24*360));
  9. if($start || $y )
  10. {
  11. $start = 1;
  12. $t12 -= $y *3600*24*360;
  13. $string .= $y."year";
  14. }
  15. $m = floor($t12/(3600*24*31));
  16. if($start || $m)
  17. {
  18. $start = 1;
  19. $t12 -= $m*3600*24*31;
  20. $string .= $m."month";
  21. }
  22. $d = floor($t12/(3600*24)) ;
  23. if($start || $d)
  24. {
  25. $start = 1;
  26. $t12 -= $d*3600*24;
  27. $string .= $d."天";
  28. }
  29. $h = floor ($t12/(3600));
  30. if($start || $h)
  31. {
  32. $start = 1;
  33. $t12 -= $h*3600;
  34. $string .= $h."hour";
  35. }
  36. $s = floor($t12/(60));
  37. if($start || $s)
  38. {
  39. $start = 1;
  40. $t12 -= $s*60;
  41. $string .= $s ."minutes";
  42. }
  43. $string .= "{$t12} seconds";
  44. echo $string;
  45. ?>
Copy code


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