Home > Backend Development > PHP Tutorial > Addition and subtraction of dates in php

Addition and subtraction of dates in php

WBOY
Release: 2016-07-29 09:02:25
Original
942 people have browsed it

Requirements: Get another date by adding or subtracting a few days to a date

1. First, get the timestamp of the date through strtotime()

2. Get the timestamp N days ago, through "current timestamp" - The number of seconds in N days = the timestamp N days ago "

3. Use the date() function to convert the format of the timestamp N days ago

The following example: get the date of the day before 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

Output: 2012-4-30

Additionally, the time() function gets the timestamp of the current date!

The above introduces the addition and subtraction of dates in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template