Home  >  Article  >  Backend Development  >  Date addition and subtraction operations in php, php addition and subtraction operations_PHP tutorial

Date addition and subtraction operations in php, php addition and subtraction operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:58:51811browse

Date addition and subtraction operations in php, php addition and subtraction operations

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1101864.htmlTechArticleAddition and subtraction of dates in php, php addition and subtraction requirements: by increasing or subtracting a certain date Go back a few days and get another date 1. First get the timestamp of the date through strtotime()...
Statement:
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