php中時(shí)間函數(shù)date及常用的時(shí)間計(jì)算
曾在項(xiàng)目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等時(shí)間戳,趁最近時(shí)間比較充足,因此計(jì)劃對php的相關(guān)時(shí)間知識點(diǎn)進(jìn)行總結(jié)學(xué)習(xí)
1,閱讀php手冊date函數(shù)
常用時(shí)間函數(shù):
checkdate()驗(yàn)證一個(gè)時(shí)間是否正確
date_default_timezone_get()取得當(dāng)前腳本所使用的時(shí)區(qū)
date_default_timezone_set()設(shè)定腳本所用時(shí)區(qū) ini_set()也可以滿足,或者修改配置文件
date_sunrise() date_sunset() 返回給定的日期和地點(diǎn)的日出時(shí)間和日落時(shí)間
date()格式化一個(gè)日期,下邊會有詳細(xì)內(nèi)容
getdate() 取得日期時(shí)間的相關(guān)信息
gettimeofday()取得當(dāng)前時(shí)間的相關(guān)信息
idate()將本地時(shí)間日期格式化為整數(shù),但只接受一個(gè)字符作為參數(shù)
microtime()返回當(dāng)前的時(shí)間戳和秒數(shù)
mktime()取得一個(gè)日期的時(shí)間戳
strtotime()將英文文本的日期秒數(shù)解析為時(shí)間戳
2,重要函數(shù)詳解
date()格式化一個(gè)日期
string date( string $format [, int $timestamp] )
d 月份中的第幾天,也就是幾號,此為具有前導(dǎo)零,例如01,02
D 星期中的第幾天,也就是英文星期幾的單詞縮寫,Mon到Sun
l(L小寫) 星期幾,此為完整的英文格式, Sunday到Saturday
N 用數(shù)字表示星期幾,1為星期一,7為星期日
S 每月天數(shù)后面的英文后綴
w 星期中的第幾天,使用數(shù)字表示,0為星期天,6為星期六
z 年份中的第幾天 0到365
W 年份中的第幾周
F 月份,完整的英文單詞
m 月份數(shù)字格式,具有前導(dǎo)0
M 三個(gè)字母表示的縮寫的月份
n 數(shù)字表示的月份,沒有前導(dǎo)0
t 給定月份所應(yīng)有的天數(shù)
L 檢測是否為閏年,閏年為1,月份為0
Y 4位數(shù)字表示的年份
y 2位數(shù)字表示的年份
a 小寫的上午或者下午的值
A 大寫的上午或者下午的值
g 12小時(shí)制,沒有前導(dǎo)0
G 24小時(shí)制,沒有前導(dǎo)0
h 12小時(shí)制,有前導(dǎo)0
H 24小時(shí)制,有前導(dǎo)0
i 具有前導(dǎo)0的分鐘數(shù)
s 秒數(shù),具有前導(dǎo)0
u 毫秒,date()函數(shù)返回的是000000格式的
e 時(shí)區(qū)標(biāo)識
I 是否為夏令時(shí),是為1,不是為0
T 本機(jī)所在的時(shí)區(qū)
c 2017-05-08T 15:22:21+00:00 格式的時(shí)間
U 從1970開始至今的秒數(shù)
idate()函數(shù)詳解
與date的區(qū)別是此函數(shù)只可以傳遞一個(gè)參數(shù),date()可以傳遞多個(gè)參數(shù)
B Internet time
d 月份中的第幾天
h 12小時(shí)制的時(shí)間
H 24小時(shí)制的時(shí)間
i 分鐘
I 若啟用夏令時(shí)返回1,否則為0
L 如果是閏年則返回1,否則返回0
m 月份的數(shù)字
s 秒數(shù)
t 本月的總天數(shù)
U 從1970起的秒數(shù)
w 星期中的第幾天
W 年份中的第幾個(gè)星期,星期從星期一開始
y 年份,1或者2位數(shù)字
Y 年份4位數(shù)字
z 年份中的第幾天
Z 以秒為單位的時(shí)區(qū)偏移量
strtotime()函數(shù)銜接
用法示例
strtotime ("now");
strtotime ("10 September 2017");
strtotime ("+1 day");
strtotime ("+1 week");
strtotime ("+1 week 2 days 4 hours 2 seconds");
strtotime ("next Thursday");
strtotime ("last Monday");
3,常用時(shí)間匯總
$times = [];
function makeTime(){
//獲取今日開始時(shí)間戳和結(jié)束時(shí)間戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
$times['today']['begin'] = $beginToday;
$times['today']['end'] = $endToday;
//獲取昨日起始時(shí)間戳和結(jié)束時(shí)間戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
$times['yesterday']['begin'] = $beginYesterday;
$times['yesterday']['end'] = $endYesterday;
//獲取上周起始時(shí)間戳和結(jié)束時(shí)間戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
$times['lastWeek']['begin'] = $beginLastweek;
$times['lastWeek']['end'] = $endLastweek;
//獲取本月起始時(shí)間戳和結(jié)束時(shí)間戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
$times['thisMonth']['begin'] = $beginThismonth;
$times['thisMonth']['end'] = $endThismonth;
//獲取本周開始時(shí)間和結(jié)束時(shí)間,此例中開始時(shí)間為周一
$defaultDate = date('Y-m-d');
$first = 1;
$w = date('w',strtotime($defaultDate));
$beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days');
$endWeek = $beginWeek + 6*24*3600-1;
$times['thisWeek']['begin'] = $beginWeek;
$times['thisWeek']['end'] = $endWeek;
//獲取上月的起始時(shí)間戳和結(jié)束時(shí)間戳
$beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y'));
$endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y'));
$times['LastMonth']['begin'] = $beginLastmonth;
$times['LastMonth']['end'] = $endLastmonth;
//獲取今年的起始時(shí)間和結(jié)束時(shí)間
$beginThisyear = mktime(0,0,0,1,1,date('Y'));
$endThisyear = mktime(23,59,59,12,31,date('Y'));
$times['thisYear']['begin'] = $beginThisyear;
$times['thisYear']['end'] = $endThisyear;
//獲取上年的起始時(shí)間和結(jié)束時(shí)間
$beginLastyear = mktime(0,0,0,1,1,date('Y')-1);
$endLastyear = mktime(23,59,59,12,31,date('Y')-1);
$times['lastYear']['begin'] = $beginLastyear;
$times['lastYear']['end'] = $endLastyear;
//獲取本季度開始時(shí)間和結(jié)束時(shí)間
$season = ceil((date('n'))/3);//當(dāng)月是第幾季度
$beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
$endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
$times['thisSeason']['begin'] = $beginThisSeason;
$times['thisSeason']['end'] = $endThisSeason;
//獲取上季度的起始時(shí)間和結(jié)束時(shí)間
$beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y'));
$endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
$times['lastSeason']['begin'] = $beginLastSeason;
$times['lastSeason']['end'] = $endLastSeason;
return $times;
}
$times = makeTime();
目前是我之前用到的時(shí)間戳,后期還會積累匯總,避免重復(fù)造輪子。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- PHP格式化顯示時(shí)間date()函數(shù)代碼
- js模仿php中strtotime()與date()函數(shù)實(shí)現(xiàn)方法
- PHP date函數(shù)常用時(shí)間處理方法
- php檢查日期函數(shù)checkdate用法實(shí)例
- php使用strtotime和date函數(shù)判斷日期是否有效代碼分享
- PHP函數(shù)之日期時(shí)間函數(shù)date()使用詳解
- PHP5.2中date()函數(shù)顯示時(shí)間與北京時(shí)間相差8小時(shí)的解決辦法
- PHP如何通過date() 函數(shù)格式化顯示時(shí)間
相關(guān)文章
PHP 利用AJAX獲取網(wǎng)頁并輸出的實(shí)現(xiàn)代碼(Zjmainstay)
PHP 利用AJAX獲取網(wǎng)頁并輸出的實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-08-08
PHP實(shí)現(xiàn)將幾張照片拼接到一起的合成圖片功能【便于整體打印輸出】
這篇文章主要介紹了PHP實(shí)現(xiàn)將幾張照片拼接到一起的合成圖片功能,可實(shí)現(xiàn)多張圖片的合并,便于整體打印輸出.涉及php字符串、數(shù)組的遍歷、排序及圖片合成、裁剪、縮放等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
利用PHP_XLSXWriter代替PHPExcel的方法示例
PHPExcel是一個(gè)處理Excel,CVS文件的開源框架,但不幸的是PHPExcel官方已不再維護(hù)了這個(gè)項(xiàng)目了,官方團(tuán)隊(duì)在github上又起了一個(gè)新項(xiàng)目,叫PhpSpreadsheet。那么這篇文章主要給大家介紹了關(guān)于利用PHP_XLSXWriter代替PHPExcel的方法示例,需要的朋友可以參考下。2017-07-07
php實(shí)現(xiàn)數(shù)組按拼音順序排序的方法
這篇文章主要介紹了php實(shí)現(xiàn)數(shù)組按拼音順序排序的方法,涉及php漢字與拼音的轉(zhuǎn)換及數(shù)組遍歷、排序相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
php實(shí)現(xiàn)求相對時(shí)間函數(shù)
這篇文章主要介紹了php實(shí)現(xiàn)求相對時(shí)間函數(shù),可實(shí)現(xiàn)簡單求相對時(shí)間為幾分鐘前或幾小時(shí)前的功能,非常簡單實(shí)用,需要的朋友可以參考下2015-06-06
ajax 的post方法實(shí)例(帶循環(huán))
在最近的項(xiàng)目中,為了能解決在大數(shù)據(jù)查詢中出現(xiàn)的超時(shí)問題,需要將大數(shù)據(jù)拆分成小數(shù)據(jù),然后進(jìn)行循環(huán)處理。本人經(jīng)驗(yàn)不足,技術(shù)有限,只能想到用ajax來傳送數(shù)據(jù)。2011-07-07

