|
|
Date Add
WE will learn how to add date time intervals to a date object. We must understand date interval and date format before using add function to the date object.
Note:This will work only if your PHP version is 5.3 or higher
Let us try to add 5 days to current date.
date_default_timezone_set ("Asia/Kolkata");
$date=new DateTime("now") ;
echo $date->format('d-m-y');
echo "<br>";
$date=$date->add(new DateInterval('P5D'));
echo $date->format('d-m-y ');
The output is here 19-05-13 24-05-13We will add 1 Year, 3 months , 5 days . We will only change the affected row in above code like this. $date=$date->add(new DateInterval('P1Y3M5D'));
echo $date->format('m-d-y ');
Let us add hour minute and seconds to this date object.
date_default_timezone_set ("Asia/Kolkata");
$date=new DateTime("now") ;
echo $date->format('d-m-y H:i:s');
echo "<br>";
$date=$date->add(new DateInterval('P1Y3M5DT4H5M12S'));
echo $date->format('m-d-y H:i:s ')";
| Click for more tutorial on Date & Time script and Applications |
|
|
| |
|
|
|
|
|