Home  >  Article  >  Backend Development  >  Implementation code for date addition and subtraction operations in PHP_PHP tutorial

Implementation code for date addition and subtraction operations in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:25959browse

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

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

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

The following example: get the date one day before 2012-5-1

Copy code The code is as follows:

//Convert time point to timestamp
$date = strtotime('2012-5 -1');
//Output the date one day ago, subtract the seconds of the day from the timestamp
echo date('Y-m-d',$date - 1*24*60*60);
?>

Output: 2012-4-30

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324668.htmlTechArticle1. First get the timestamp of the date through strtotime() 2. Get the timestamp N days ago, pass" The current timestamp - the number of seconds in N days = the timestamp N days ago" 3. For the timestamp N days ago, use da...
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