Home > Article > Backend Development > Date addition and subtraction operations in php, php addition and subtraction operations_PHP tutorial
Requirement: Get another date by adding or subtracting a few days to a certain date
1. First obtain the timestamp of the date through strtotime()
2. Obtain the timestamp N days ago, through "current timestamp - number of seconds in N days = 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 one 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
In addition, the time() function gets the timestamp of the current date!