php 獲取今日、昨日、上週、本月的起始時間戳和結束時間戳的方法

https://www.cnblogs.com/rainblack/p/4924581.html
php


php 獲取今日、昨日、上週、本月的起始時間戳和結束時間戳的方法,主要使用到了 php 的時間函數 mktime。下面首先仍是直奔主題以示例說明如何使用 mktime 獲取今日、昨日、上週、本月的起始時間戳和結束時間戳,而後在介紹一下 mktime 函數做用和用法。html

//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 mktime() 函數用於返回一個日期的 Unix 時間戳。ide

語法函數

mktime(hour,minute,second,month,day,year,is_dst)this


參數 描述
hour 可選。規定小時。
minute 可選。規定分鐘。
second 可選。規定秒。
month 可選。規定用數字表示的月。
day 可選。規定天。
year 可選。規定年。在某些系統上,合法值介於 1901 - 2038 之間。不過在 PHP 5 中已經不存在這個限制了。
is_dst

可選。若是時間在日光節約時間(DST)期間,則設置爲1,不然設置爲0,若未知,則設置爲-1。spa

自 5.1.0 起,is_dst 參數被廢棄。所以應該使用新的時區處理特性。code


用法htm

參數老是表示 GMT 日期,所以 is_dst 對結果沒有影響。blog

參數能夠從右到左依次空着,空着的參數會被設爲相應的當前 GMT 值。get

注意在 PHP 5.1 以前,若是該函數的參數非法,則會返回 false。

另外須要注意的是該函數對於日期運算和驗證很是有用。它能夠自動校訂越界的輸入,如:

1 echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));

將輸出結果如:

Jan-05-2002

2、

    

//獲取今天00:00

$todaystart = strtotime(date('Y-m-d'.'00:00:00',time()));
 //獲取今天24:00
$todayend = strtotime(date('Y-m-d'.'00:00:00',time()+3600*24));
//統計今天註冊的用戶
$todayuser['create_time'] = array(between,"$todaystart,$todayend");
$todaysum = $Users->where($todayuser)->count();

//獲取昨天00:00 $timestart = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24)); //獲取今天00:00 $timeend = strtotime(date('Y-m-d'.'00:00:00',time())); //統計昨天註冊的用戶 $map['create_time'] = array(between,"$timestart,$timeend"); $daycount = $Users->where($map)->count();   $this->assign("todaysum",$todaysum); $this->assign("daycount",$daycount);

相關文章
相關標籤/搜索