Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime(), date_add() in PHP.
Method 1: Using strtotime() Function: The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp.
Syntax:
strtotime( $EnglishDateTime, $time_now )
Parameters: This function accepts two optional parameters as mentioned above and described below.
- $EnglishDateTime: This parameter specifies the English textual date-time description, which represents the date or time to be returned.
- $time_now: This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.
Program: PHP program to add days to $Date in PHP using strtotime() function.
php
<?php
$Date = "2019-05-10" ;
echo date ( 'Y-m-d' , strtotime ( $Date . ' + 10 days' ));
?>
|
Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds.
Syntax:
date_add(object, interval);
Parameters: This function accepts two parameters as mentioned above and described below:
- Object: It specifies the DateTime object returned by date_create() function.
- Interval: It specifies the DateInterval object.
Program: PHP program to add days to $Date in PHP using date_add() function.
php
<?php
$date = date_create( "2019-05-10" );
date_add( $date , date_interval_create_from_date_string( "10 days" ));
echo date_format( $date , "Y-m-d" );
?>
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!