PHP輸出時(shí)間差函數(shù)代碼
PHP輸出時(shí)間差函數(shù)
<?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í)間段的日期。現(xiàn)在已經(jīng)進(jìn)行收集,大家同時(shí)也可以進(jìn)行擴(kuò)展豐富
//獲取當(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)在過(guò)幾月的的時(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和MySql中計(jì)算時(shí)間差的方法詳解
- php計(jì)算兩個(gè)日期時(shí)間差(返回年、月、日)
- 在php和MySql中計(jì)算時(shí)間差的方法
- php中計(jì)算時(shí)間差的幾種方法
- php將時(shí)間差轉(zhuǎn)換為字符串提示
- php中將時(shí)間差轉(zhuǎn)換為字符串提示的實(shí)現(xiàn)代碼
- php 8小時(shí)時(shí)間差的解決方法小結(jié)
- php獲取給定日期相差天數(shù)的方法分析
- php判斷兩個(gè)日期之間相差多少個(gè)月份的方法
- php計(jì)算兩個(gè)日期相差天數(shù)的方法
- PHP簡(jiǎn)單計(jì)算兩個(gè)時(shí)間差的方法示例
相關(guān)文章
解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
本篇文章是對(duì)mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php-accelerator網(wǎng)站加速PHP緩沖的方法
我們知道 Zend 有免費(fèi)的優(yōu)化引擎針對(duì) PHP 而作,但是 FreeLAMP 這次采用的是一個(gè)叫做 PHP Accelerator 的緩沖產(chǎn)品。2008-07-07windows下配置apache+php+mysql時(shí)出現(xiàn)問(wèn)題的處理方法
windows下配置apache+php+mysql應(yīng)該是每個(gè)phper必須掌握的基礎(chǔ)技能了,這也是熟悉php的一個(gè)過(guò)程,小編當(dāng)年自己配環(huán)境的時(shí)候也遇到過(guò)這樣那樣的問(wèn)題,現(xiàn)在把當(dāng)時(shí)記錄的幾個(gè)問(wèn)題的處理方法分享給大家2014-06-06PHP+Mysql日期時(shí)間如何轉(zhuǎn)換(UNIX時(shí)間戳和格式化日期)
UNIX時(shí)間戳和格式化日期是我們常打交道的兩個(gè)時(shí)間表示形式,Unix時(shí)間戳存儲(chǔ)、處理方便,但是不直觀,格式化日期直觀,但是處理起來(lái)不如Unix時(shí)間戳那么自如,所以有的時(shí)候需要互相轉(zhuǎn)換,下面給出互相轉(zhuǎn)換的幾種轉(zhuǎn)換方式2012-07-07PHP常見(jiàn)數(shù)組函數(shù)用法小結(jié)
這篇文章主要介紹了PHP常見(jiàn)數(shù)組函數(shù)用法,結(jié)合實(shí)例形式分析了array_merge、array_slice及array_map函數(shù)的使用技巧,需要的朋友可以參考下2016-03-03