|
php輸出時(shí)間差函數(shù)
復(fù)制代碼 代碼如下:
<?php
date_default_timezone_set('PRC'); //默認(rèn)時(shí)區(qū)
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 "一周零兩天四小時(shí)兩秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
echo "下個(gè)星期四:",date("Y-m-d",strtotime("next Thursday")), "<br>";
echo "上個(gè)周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一個(gè)月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一個(gè)月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
?>
在學(xué)習(xí)php 的時(shí)候,經(jīng)常會(huì)用到獲取現(xiàn)在之前或之后,某個(gè)時(shí)間段的日期?,F(xiàn)在已經(jīng)進(jìn)行收集,大家同時(shí)也可以進(jìn)行擴(kuò)展豐富
復(fù)制代碼 代碼如下:
//獲取當(dāng)天的星期(1-7)
function GetWeek($times)
{
$res = date('w', strtotime($times));
if($res==0)
$res=7;
return $res;
}
//獲取當(dāng)天時(shí)間
function GetTime($times)
{
$res = date('H:i', strtotime($times));
return $res;
}
//獲取現(xiàn)在過幾月的的時(shí)間
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;
}
//獲取當(dāng)前時(shí)間
function GetCurrentDateTime()
{
$res=date("Y-m-d H:i:s",time());
return $res;
}
//獲取當(dāng)前時(shí)間隔幾小時(shí)之前或之后的時(shí)間
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;
}
//間隔幾分鐘之前或之后的時(shí)間
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;
}
//間隔幾秒之前或之后的時(shí)間
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;
}
//間隔幾個(gè)星期之前或之后的時(shí)間
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;
}
// 間隔幾天之間的時(shí)間
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;
}
//間隔幾年之前或之后的時(shí)間
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;
}
php技術(shù):PHP輸出時(shí)間差函數(shù)代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。