Implementation code for date addition and subtraction in php

  • 2020-05-10 17:47:02
  • OfStack

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

2. Get the time stamp of N days ago by "current time stamp - seconds of N days = N days ago time stamp"

3. Format conversion with date() function for the time stamp of N days ago

The date one day before the date of the date of May 1, 2012
 
<?php 
// Converts the time point to a timestamp  
$date = strtotime('2012-5-1'); 
// The output 1 The day before, subtract from the timestamp 1 The number of seconds a day  
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!

Related articles: