今天我們來看在php中計算兩個時間之間的時間差.下面我們直接使用了data,strtotime與time三個函數就實現了.有需要的朋友可參考,今天要講的這個例子,需求是這樣的,得知某個日期時間,如:2012-04-25 10:10:00,我要在這個日期時間的基礎上加上5個月并返回處理后的日期,結果:2012-04-25 10:10:00 加5個月等于 2012-09-25 10:10:00
結合PHP函數date()與strtotime()兩個函數來實現大致也是這個意思,代碼如下:
- <?php
- /**
- * PHP里的日期加減方法
- * 瓊臺老屋
- */
- // 第一步,假設有一個時間
- $a = '2012-04-25 10:10:00';
- // 第二步,獲得這個日期的時間戳
- $a_time = strtotime($a);
- // 第三步,獲得加五個月后的時間戳
- $b_time = strtotime('+5 Month',$a_time);
- // 第四部,把時間戳轉換回日期格式
- $b = date('Y-m-d H:i:s',$b_time);
- echo '這是加了五個月后的日期'.$b;
- // 如果你覺得以上代碼過長也可以一行搞定
- $b = date('Y-m-d H:i:s',strtotime('+'.$time.' Month',strtotime($a)));
- echo '這是加了五個月后的日期'.$b;
- ?>
常用的計算時間,代碼如下:
- <?php
- date_default_timezone_set('PRC'); //默認時區
- echo "今天:",date("Y-m-d",time()),"<br>";
- echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<br>";
- echo "昨天:",date("Y-m-d",strtotime("-1 day")), "<br>";
- echo "明天:",date("Y-m-d",strtotime("+1 day")), "<br>";
- echo "一周后:",date("Y-m-d",strtotime("+1 week")), "<br>";
- echo "一周零兩天四小時兩秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
- echo "下個星期四:",date("Y-m-d",strtotime("next Thursday")), "<br>";
- echo "上個周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
- echo "一個月前:".date("Y-m-d",strtotime("last month"))."<br>";
- echo "一個月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
- echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
- ?>
- //輸出結果
- //今天:2013-06-07
- //今天:2008-06-18
- //昨天:2013-06-06
- //明天:2013-06-08
- //一周后:2013-06-14
- //一周零兩天四小時兩秒后:2013-06-16 18:18:29
- //下個星期四:2013-06-13
- //上個周一:2013-06-03
- //一個月前:2013-05-07
- //一個月后:2013-07-07
- //十年后:2023-06-07
這里再看一些日期加減函數,代碼如下:
- //獲取當天的星期(1-7)
- function GetWeek($times)
- {
- $res = date('w', strtotime($times));
- if($res==0)
- $res=7;
- return $res;
- }
- //獲取當天時間
- function GetTime($times)
- {
- $res = date('H:i', strtotime($times));
- return $res;
- }
- //獲取現在過幾月的的時間
- function GetMonth($Month,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$Month months"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$Month months"));
- return $res;
- }
- //獲取當前時間
- function GetCurrentDateTime()
- {
- $res=date("Y-m-d H:i:s",time());
- return $res;
- }
- //獲取當前時間隔幾小時之前或之后的時間
- function GetDiffHours($hours,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$hours hour"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$hours hour"));
- return $res;
- }
- //間隔幾分鐘之前或之后的時間
- function GetDiffMinute($Minute,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$Minute minute"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$Minute minute"));
- return $res;
- }
- //間隔幾秒之前或之后的時間
- function GetDiffSec($sec,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$sec second"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$sec second"));
- return $res;
- }
- //間隔幾個星期之前或之后的時間
- function GetDiffWeek($Week,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$Week week"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$Week week"));
- return $res;
- }
- // 間隔幾天之間的時間
- function GetDiffDays($days,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$days day"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$days day"));
- return $res;
- }
- //間隔幾年之前或之后的時間
- function GetDiffYears($year,$type='l')
- {
- if(!strcmp($type,'b'))
- $res=date("Y-m-d H:i:s",strtotime("-$year year"));
- if(!strcmp($type,'l'))
- $res=date("Y-m-d H:i:s",strtotime("+$year year"));
- return $res;
- }
新聞熱點
疑難解答