增加方法:1、使用“strtotime('日期-月份-天數(shù)')”語句將指定日期轉(zhuǎn)為時(shí)間戳;2、將需要添加的天數(shù)轉(zhuǎn)為秒數(shù)值,語法“天數(shù) *24 * 3600”;3、使用“date('Y-m-d',時(shí)間戳+秒數(shù)值)”語句給日期增加指定天數(shù)。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php給指定日期增加天數(shù)
實(shí)現(xiàn)思想:
-
將指定日期轉(zhuǎn)為時(shí)間戳;
-
將需要添加的天數(shù)轉(zhuǎn)為秒數(shù)值;
-
時(shí)間戳和秒數(shù)相加形成新的時(shí)間戳;
-
將新時(shí)間戳轉(zhuǎn)為日期格式。
實(shí)現(xiàn)代碼:
<?php header("Content-type:text/html;charset=utf-8"); $start = strtotime('2022-2-13');; $interval = 2 * 24 * 3600;//2天 echo date('Y-m-d',$start + $interval); ?>
說明:
strtotime() 函數(shù)將任何英文文本的日期或時(shí)間描述解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù))。
date() 函數(shù)格式化本地日期和時(shí)間,并返回已格式化的日期字符串。
推薦學(xué)習(xí):《PHP視頻教程》