Home >Backend Development >PHP Tutorial >Addition and subtraction of dates in php
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 />?>
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.