在php的一些數(shù)據(jù)統(tǒng)計(jì)查詢邏輯中,可能需要用到以時(shí)間段為條件的查詢。這篇文章飛鳥慕魚就來說一說,在php中如何獲取當(dāng)天的開始時(shí)間戳以及結(jié)束的時(shí)間戳。
php 獲取當(dāng)天開始與結(jié)束時(shí)間戳的方法

一天的開始時(shí)間,一般從0點(diǎn)0分0秒開始,到23到59分59秒結(jié)束(別噴我,網(wǎng)上是這么說的,當(dāng)然還有不同的說法),我們只要利用 php 中的時(shí)間處理函數(shù)來組合這個(gè)時(shí)段段就可以了,下面介紹了幾種簡單而常用的方法,喜歡哪個(gè)就用哪個(gè)吧。
方法1:
<?php $s = strtotime(date('Y-m-d').'00:00:00'); $l = strtotime(date('Y-m-d').'23:59:59'); //輸出開始的時(shí)間戳與日期 echo $s; echo date('Y-m-d H:i:s',$s); //輸出結(jié)束的時(shí)間戳與日期 echo $l; echo date('Y-m-d H:i:s',$l); ?>
輸出結(jié)果:
1572537600 2019-11-01 00:00:00 1572623999 2019-11-01 23:59:59
方法2:
<?php $s = strtotime(date("Y-m-d",time())); $l = $s+60*60*24-1; //輸出開始的時(shí)間戳與日期 echo $s; echo date('Y-m-d H:i:s',$s); //輸出結(jié)束的時(shí)間戳與日期 echo $l; echo date('Y-m-d H:i:s',$l); ?>
方法3:
<?php $s = mktime(0,0,0,date("m"),date("d"),date("Y")); $l = mktime(23,59,59,date("m"),date("d"),date("Y")); //輸出開始的時(shí)間戳與日期 echo $s; echo date('Y-m-d H:i:s',$s); //輸出結(jié)束的時(shí)間戳與日期 echo $l; echo date('Y-m-d H:i:s',$l); ?>