Friday, October 10, 2025
HomeLanguagesPHP | date_create(), date_format(), date_add() Functions

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:  

  1. 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.
  2. 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. 

  1. object : Specifies a DateTime object returned by date_create()
  2. 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.  

  1. Object : Specifies a DateTime object returned by date_create(). This function returns a new DateTime object.
  2. 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

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS