PHP | date_create(), date_format(), date_add() Functions
Last Updated :
13 Jan, 2022
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
<?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
// 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
//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:
Similar Reads
PHP | date_create_from_format() Function
The date_create_from_format() is an inbuilt function in php which is used to parses a time string according to a specified format. This function accepts three parameters and returns new DateTime in success or false on failure. Syntax: Procedural style date_create_from_format ( $format, $time, $timez
3 min read
PHP | DateTime format() Function
The DateTime::format() function is an inbuilt function in PHP which is used to return the new formatted date according to the specified format. Syntax: Object oriented style string DateTime::format( string $format ) or string DateTimeImmutable::format( string $format ) or string DateTimeInterface::f
1 min read
PHP | date_create_immutable_from_format() Function
The date_create_immutable_from_format() function is an inbuilt function in PHP which is used to parse a time string according to the specified format. Syntax: Object oriented style: DateTimeImmutable static DateTime::createFromFormat( string $format, string $time, DateTimeZone $timezone )Procedural
2 min read
PHP | DateTime createFromFormat() Function
The DateTime::createFromFormat()function is an inbuilt function in PHP which returns a new DateTime object representing the date and time format. Syntax: Object oriented style: DateTime DateTime::createFromFormat( string $format, string $time, DateTimeZone $timezone ) Procedural style: DateTime date
3 min read
PHP | easter_date() Function
The easter_date() function is a built-in function in PHP which returns the Easter date in the year passed as an argument. The current year is taken as default year when no arguments are passed as parameter. Syntax: easter_date( $year ) Parameter: The function accepts one optional parameter $year whi
2 min read
PHP | DateTime add() Function
The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object. Syntax: Object oriented style: DateTime DateTime::add( DateInterval $interval ) Procedural style: DateTime date_add( DateT
2 min read
PHP | date_parse_from_format() Function
The date_parse_from_format() is an inbuilt function in PHP which is used to get information about given date formatted according to the specified format. The date_parse_from_format() function accepts two parameters and returns associative array with detailed information about given date. Syntax: arr
3 min read
PHP | DateTimeImmutable add() Function
The DateTimeImmutable::add() function is an inbuilt function in PHP which is used to add a number of days, months, years, hours, minutes and seconds to a created DateTimeImmutable object. Syntax: DateTimeImmutable DateTimeImmutable::add( DateInterval $interval ) Parameters: This function accepts a s
2 min read
PHP | date_date_set() Function
The date_date_set() function is an inbuilt function in PHP which is used to set a new Date. This function has four parameters $object, $year, $month and $day and returns DateTime object on success or false on failure. The date_date_set() function is an alias of DateTime::setDate() function. Syntax:
2 min read
PHP | DateTime diff() Function
The DateTime::diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects. Syntax: Object oriented style: DateInterval DateTime::diff( DateTimeInterface $datetime2, bool $absolute = FALSE ) or DateInterval DateTimeImmutable::diff( DateTimeI
2 min read