PHP日期時間加減程式碼詳解

來源:互聯網
上載者:User

今天要講的這個例子,需求是這樣的。得知某個日期時間,

如: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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.