P粉3930309172023-08-21 13:28:25
If you are using PHP 5.3, you can use the DateTime
object and its add
method:
$Date1 = '2010-09-17'; $date = new DateTime($Date1); $date->add(new DateInterval('P1D')); // P1D表示1天的时间段 $Date2 = $date->format('Y-m-d');
See the DateInterval
Constructor manual page for how to construct other time periods to add to your date (e.g. 2 days for 'P2D'
, 3 days for 'P3D'
, etc.).
If you don't have PHP 5.3, you should be able to use strtotime
as you did before (I've tested this and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17'; $Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day")); // var_dump($Date2)返回"2010-09-18"
P粉2587888312023-08-21 09:02:28
You just need to use days
instead of day
like this:
<?php $Date = "2010-09-17"; echo date('Y-m-d', strtotime($Date. ' + 1 days')); echo date('Y-m-d', strtotime($Date. ' + 2 days')); ?>
It will correctly output:
2010-09-18 2010-09-19