PHP | date_create(), date_format(), date_add() Functions
There is some point in time when we need to add a number of days, months, years, hours, minutes and seconds to Date and time. PHP serves us with several built-in functions to do this. Some built-in functions that we will discuss here are date_create(), date_format() and date_add().
date_create() Function
This function is used to create a DateTime object by using a date/time string and timezone. The default value of the date/time string is current date/time.
Syntax:
DateTime date_create(time, timezone);
Parameters:This function accepts two parameters:
- time : (optional) Specifies a date/time string. NULL or default value
indicates the current date/time. You may refer to this link for supported date and time formats in PHP. - timezone : (optional) Time zone of the time.
Return Value: This function returns a new DateTime object which specifies a date.
date_format() Function
The date_format() function formats a given date. The date is supplied as DateTime instance which is generally returned by the date_create() function and format is a string according to which we want to format the date.
Syntax:
string date_format(object, format);
Parameters : This function accepts two parameters, all of this which are mandatory to be supplied.
- object : Specifies a DateTime object returned by date_create()
- format : Specifies the format for the date. It accepts the formats that is supported by date() function in PHP. Example - H(24-hr Format), h(12-hr Format), i(minutes:00 to 59), s(seconds: 00 to 59) etc.
Return Value: The date_format() function returns a string which represents the date formatted according to the specified format on successful formatting otherwise it returns false on failure.
<?php
// using date_create() function to create
// DateTime object
$date=date_create("2018-03-15");
// using date_format() function to format date
echo date_format($date, "Y/m/d H:i:s");
?>
<?php
// using date_create() function to create
// DateTime object
$date=date_create("2018-03-15");
// using date_format() function to format date
echo date_format($date, "Y/m/d H:i:s");
?>
Output:
2018/03/15 00:00:00
date_add() Function
The date_add() function is used to add days, months, years, hours, minutes and seconds to a Date. The Date is supplied as a DateTime object to the date_add() function and the interval which we want to add to our Date is supplied as a DateInterval object.
Syntax:
DateTime date_add(object, interval);
Parameters:This function accepts three parameters, all of this which are mandatory to be supplied.
- Object : Specifies a DateTime object returned by date_create(). This function returns a new DateTime object.
- Interval : Specifies a DateInterval object i.e it stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports.
Return Value : This function returns a DateTime object on success else FALSE on failure.
Below programs illustrate the date_add() function in PHP:
Example-1
<?php
// PHP program to add 40 days in date
$date=date_create("2018-12-10");
date_add($date, date_interval_create_from_date_string("40 days"));
echo date_format($date, "Y-m-d");
?>
<?php
// PHP program to add 40 days in date
$date=date_create("2018-12-10");
date_add($date, date_interval_create_from_date_string("40 days"));
echo date_format($date, "Y-m-d");
?>
Output:
2019-01-19
Example-2
<?php
//PHP program to add 1 year, 10 mins, 23 secs in date
$date=date_create("2018-12-10");
date_add($date, date_interval_create_from_date_string("1 year
+ 10 mins + 23 secs"));
echo date_format($date, "Y-m-d H:i:s");
?>
<?php
//PHP program to add 1 year, 10 mins, 23 secs in date
$date=date_create("2018-12-10");
date_add($date, date_interval_create_from_date_string("1 year
+ 10 mins + 23 secs"));
echo date_format($date, "Y-m-d H:i:s");
?>
Output:
2019-12-10 00:10:23
Note : Using '+' operator we can add more to date and time.
References:
- http://php.net/manual/en/datetime.format.php
- http://php.net/manual/en/function.date-add.php
- http://php.net/manual/en/datetime.construct.php