-
- function count_days($a,$b){
- $a_dt=getdate($a);
- $b_dt=getdate($b);
- $a_new=mktime(12,0, 0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']);
- $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt[ 'mday'],$b_dt['year']);
- return round(abs($a_new-$b_new)/86400);
- }
- //How many days are there between today and October 11, 2008
- $date1= strtotime(time());
- $date1=strtotime('10/11/2008');
- $result=count_days($date1,$date2);
- echo $result;
- ?>
Copy code
Method 2:
-
- //How many days are there between today and September 9, 2008
- $Date_1=date("Y-m-d");
- $Date_2="2008-10-11";
- $d1 =strtotime($Date_1);
- $d2=strtotime($Date_2);
- $Days=round(($d2-$d1)/3600/24);
- echo "The difference between today and October 11, 2008". $Days."Days";
- ?>
Copy code
PHP Get the time difference between any time and the current time
Code:
-
- #Function: Get the time difference between any time and the current time
- function QueryDays($datestr){
- #Format time
- $da=preg_split("/(-| |:)/i ",$datestr);
- $nowyear=date("Y");
- $nowmon=date("n");
- $nowday=date("d");
- $nowtimes=mktime(0,0,0 ,$nowmon,$nowday,$nowyear);
- $pdtimes= mktime(0,0,0,$nowmon,$nowday,$nowyear-1);
- $bjtimes= mktime(0,0,0,$da[ 1],$da[2],$da[0]);
- #Judge whether the time given is within one year
- if ($bjtimes>=$pdtimes and $bjtimes<=$nowtimes){
- return ( floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[ 1],$da[2],$da[0]))));
- }else{
- $loop=$nowyear-$da[0];
- $totaldays=(floor(strftime("%j", mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
- for($i=1;$i<=$ loop;$i++){
- for($j=12;$j>=1;$j--){
- if ($da[0]==$nowyear-$i and $da[1]==$ j){
- $days=MonDays($nowyear-$i,$j);
- return $totaldays+=$days-$da[2];
- break;
- }else{
- $days=MonDays($nowyear-$ i,$j);
- $totaldays+=$days;
- }//end else
- }//end for
- }//end for
- }//end else
- }//end function
- #Get the number of days in the month
- function MonDays($year,$month){
- switch ($month){
- case "1":
- case "3":
- case "5":
- case "7":
- case "8":
- case "10":
- case "12": $days=31;break;
- case "4":
- case "6":
- case "9":
- case "11": $days=30;break;
- case "2":
- if (checkdate($month,29,$year)){
- $days=29;
- }else{
- $days=28;
- }//end else
- break;
- }//end switch
- return $days;
- }//end function
- $datestr="2002-1-14 9:47:20";
- echo QueryDays($datestr);
- ?>
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:
-
- $time1 = "2008-6-15 11:49:59"; //The first time
- $time2 = "2007-5-5 12:53:28"; //The second time
- $t1 = strtotime($time1);
- $t2 = strtotime($time2);
- $t12 = abs($t1-$t2);
- $start = 0;
- $string = " The difference between the two times: ";
- $y = floor($t12/(3600*24*360));
- if($start || $y )
- {
- $start = 1;
- $t12 -= $y *3600*24*360;
- $string .= $y."year";
- }
- $m = floor($t12/(3600*24*31));
- if($start || $m)
- {
- $start = 1;
- $t12 -= $m*3600*24*31;
- $string .= $m."month";
- }
- $d = floor($t12/(3600*24)) ;
- if($start || $d)
- {
- $start = 1;
- $t12 -= $d*3600*24;
- $string .= $d."天";
- }
- $h = floor ($t12/(3600));
- if($start || $h)
- {
- $start = 1;
- $t12 -= $h*3600;
- $string .= $h."hour";
- }
- $s = floor($t12/(60));
- if($start || $s)
- {
- $start = 1;
- $t12 -= $s*60;
- $string .= $s ."minutes";
- }
- $string .= "{$t12} seconds";
- echo $string;
- ?>
Copy code
|