不管是進行統計還是在平常的php使用過程中需要使用時間轉換的有很多,在這裏簡單做一下總結:
一、php中時間轉換函數
二、實例1.strtotime()
語法:strtotime(time,now);
參數:time:必需,規定日期時間字符串
now:可選,規定用來計算返回值的時間戳.如果省略該參數,則使用當前時間
2.date()
語法:date(format,timestamp)
參數:format:必需,規定時間戳的格式
timestamp:可選,規定時間戳,默認是當前時間和日期
1.簡單示例:使用php獲得今天零點的時間戳
$todaytime=strtotime("today"),
可以再使用data("Y-m-d H:i",$todaytime)轉換爲日期
2.使用實例:php獲得今日,昨日,上週,本月,本季度,本年的起始時間戳和結束時間戳
php獲取今日開始時間戳和結束時間戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
php獲取昨日開始時間戳和結束時間戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
php獲取上週開始時間戳和結束時間戳
$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'));
php獲取本月開始時間戳和結束時間戳$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
php獲取本季度開始時間戳和結束時間戳$getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));//本季度未最後一月天數
$beginQuarter=mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y'));
$endQuarter=mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y'));
php獲取本年開始時間戳和結束時間戳
$beginYear=strtotime(date('Y-01-01',time())),
$endYear=strtotime(date('Y-01-01',time()).'+1 year');
或者
$startYeartime=date('Y-01-01',time());
$endYeartime=date('Y-12-31',time());
$startYear=strtotime("{$startYeartime} 00:00:00");
$endYear=strtotime("{$endYeartime} 23:59:59");
php獲取去年開始時間戳和結束時間戳
$beginLastyear=mktime(0,0,0,1,1,date('Y',strtotime("-1 year")));
$endLastyear=mktime(23,59,59,12,date('t',strtotime("December")),date('Y',strtotime("-1 year")));
轉載自:php程序員的筆記
地址:http://www.phpernote.com/php-function/635.html