PHP輸出時間差函數(shù)代碼
PHP輸出時間差函數(shù)
<?php
date_default_timezone_set('PRC'); //默認時區(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 "一周零兩天四小時兩秒后:",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>";
?>
在學習PHP 的時候,經(jīng)常會用到獲取現(xiàn)在之前或之后,某個時間段的日期?,F(xiàn)在已經(jīng)進行收集,大家同時也可以進行擴展豐富
//獲取當天的星期(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;
}
//獲取現(xiàn)在過幾月的的時間
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;
}
相關(guān)文章
解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
本篇文章是對mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06php-accelerator網(wǎng)站加速PHP緩沖的方法
我們知道 Zend 有免費的優(yōu)化引擎針對 PHP 而作,但是 FreeLAMP 這次采用的是一個叫做 PHP Accelerator 的緩沖產(chǎn)品。2008-07-07windows下配置apache+php+mysql時出現(xiàn)問題的處理方法
windows下配置apache+php+mysql應該是每個phper必須掌握的基礎(chǔ)技能了,這也是熟悉php的一個過程,小編當年自己配環(huán)境的時候也遇到過這樣那樣的問題,現(xiàn)在把當時記錄的幾個問題的處理方法分享給大家2014-06-06PHP+Mysql日期時間如何轉(zhuǎn)換(UNIX時間戳和格式化日期)
UNIX時間戳和格式化日期是我們常打交道的兩個時間表示形式,Unix時間戳存儲、處理方便,但是不直觀,格式化日期直觀,但是處理起來不如Unix時間戳那么自如,所以有的時候需要互相轉(zhuǎn)換,下面給出互相轉(zhuǎn)換的幾種轉(zhuǎn)換方式2012-07-07