Home > php教程 > php手册 > body text

php中日期的加减法运算,php加减法运算

WBOY
Release: 2016-06-13 08:45:57
Original
1154 people have browsed it

php中日期的加减法运算,php加减法运算

需求:通过对某个日期增加或减去几天,得到另外一个日期

1、首先通过strtotime()获得日期的时间戳

2、获得N天前得时间戳,通过”当前时间戳 - N天的秒数 = N天前得时间戳“

3、对N天前得时间戳用date()函数进行格式转换

下例:获得2012-5-1号之前一天的日期

<?php<br />//将时间点转换为时间戳<br />$date = strtotime('2012-5-1');<br />//输出一天前的日期,在时间戳上减去一天的秒数<br />echo date('Y-m-d',$date - 1*24*60*60);<br />?>
Copy after login

输出:2012-4-30

此外,time()函数获得当前日期的时间戳!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!