這篇文章主要介紹“PHP怎么返回某個日期的前一天和后一天”,在日常操作中,相信很多人在PHP怎么返回某個日期的前一天和后一天問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PHP怎么返回某個日期的前一天和后一天”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司主要為客戶提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁視覺設(shè)計、VI標(biāo)志設(shè)計、成都全網(wǎng)營銷推廣、網(wǎng)站程序開發(fā)、HTML5響應(yīng)式網(wǎng)站建設(shè)、手機(jī)網(wǎng)站開發(fā)、微商城、網(wǎng)站托管及企業(yè)網(wǎng)站維護(hù)、WEB系統(tǒng)開發(fā)、域名注冊、國內(nèi)外服務(wù)器租用、視頻、平面設(shè)計、SEO優(yōu)化排名。設(shè)計、前端、后端三個建站步驟的完善服務(wù)體系。一人跟蹤測試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為成都葡萄架行業(yè)客戶提供了網(wǎng)站營銷服務(wù)。
本文的重點(diǎn)是:返回給定時間的前一天、后一天的日期。那么要怎么操作呢?
其實(shí)很簡單,PHP內(nèi)置的strtotime() 函數(shù)就可以實(shí)現(xiàn)這個操作!下面來看看我的實(shí)現(xiàn)方法:
返回某個日期的前一天的實(shí)現(xiàn)代碼
<?php
function GetTime($year,$month,$day){
$timestamp = strtotime("{$year}-{$month}-{$day}");
$time = strtotime("-1 days",$timestamp);
echo date("Y-m-d",$time)."<br>";
}
GetTime(2000,3,1);
GetTime(2021,1,1);
?>輸出結(jié)果:

返回某個日期的后一天的實(shí)現(xiàn)代碼
<?php
function GetTime($year,$month,$day){
$timestamp = strtotime("{$year}-{$month}-{$day}");
$time = strtotime("+1 days",$timestamp);
echo date("Y-m-d",$time)."<br>";
}
GetTime(2000,2,28);
GetTime(2021,2,28);
?>輸出結(jié)果:

分析一下關(guān)鍵代碼:
strtotime() 函數(shù)有兩種用法:一種是將字符串形式的、用英文文本描述的日期時間解析為 UNIX 時間戳,一種是用來計算一些日期時間的間隔。
我們利用strtotime() 函數(shù)計算時間間隔的功能,使用strtotime("-1 days",$timestamp)和strtotime("+1 days",$timestamp)計算出指定日期前一天和后一天的日期。
"-1 days"就是減一天,"+1 days"就是加一天;觀察規(guī)律,我們還可以根據(jù)需要獲取前N天,后N天的日期
<?php
function GetTime($year,$month,$day){
$timestamp = strtotime("{$year}-{$month}-{$day}");
$time1 = strtotime("-2 days",$timestamp);
$time2 = strtotime("+3 days",$timestamp);
echo date("Y-m-d",$time1)."<br>";
echo date("Y-m-d",$time2)."<br>";
}
GetTime(2000,3,5);
?>
當(dāng)strtotime() 函數(shù)有兩個參數(shù)時,第二個參數(shù)必須是時間戳格式。所以我們需要先使用一次 strtotime()函數(shù)將字符串形式的指定日期轉(zhuǎn)為字符串;在使用一次 strtotime()函數(shù)進(jìn)行日期的加減運(yùn)算,獲取算前N天和后N天的日期。
strtotime() 函數(shù)的返回值是時間戳格式的;所以需要使用date("Y-m-d",$time)來格式化日期時間,返回年-月-日格式的日期。
擴(kuò)展知識:
其實(shí)利用strtotime() 函數(shù),不僅可以獲取前N天和后N天日期,還可以獲取前N月和后N月日期、前N年和后N年日期:
<?php
$month2 = strtotime("-1 months",strtotime("2000-1-2"));
$month3 = strtotime("+2 months",strtotime("2000-1-2"));
echo date("Y-m-d",$month2)."<br>";
echo date("Y-m-d",$month3)."<br><br>";
$year1 = strtotime("-1 years",strtotime("2000-1-2"));
$year2 = strtotime("+2 years",strtotime("2000-1-2"));
echo date("Y-m-d",$year1)."<br>";
echo date("Y-m-d",$year2)."<br>";
?>輸出結(jié)果:

想要獲取前一周和后一周的日期,也可以利用strtotime() 函數(shù)。例如:當(dāng)前日期2021-8-19,前一周和后一周的日期為:

實(shí)現(xiàn)代碼:
<?php
header("content-type:text/html;charset=utf-8");
$start = time(); //獲取當(dāng)前時間的時間戳
echo "當(dāng)前日期為:".date('Y-m-d',$start)."<br />";
$interval = 7 * 24 * 3600; //一周總共的秒數(shù)
$previous_week = $start - $interval; //當(dāng)前時間的時間戳 減去 一周總共的秒數(shù)
$next_week = $start + $interval; //當(dāng)前時間的時間戳 加上 一周總共的秒數(shù)
echo "前一周日期為:".date('Y-m-d',$previous_week)."<br />";
echo "后一周日期為:".date('Y-m-d',$next_week)."<br />";
?>輸出結(jié)果:

前后兩個日期正好相差 7 天。這其實(shí)就是計算時間差的一種逆運(yùn)用。
到此,關(guān)于“PHP怎么返回某個日期的前一天和后一天”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站名稱:PHP怎么返回某個日期的前一天和后一天
標(biāo)題來源:http://chinadenli.net/article32/jggspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、品牌網(wǎng)站制作、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)