Open In App
Related Articles

PHP | date_create(), date_format(), date_add() Functions

Improve Article
Save Article
Like Article

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. 

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.

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. 


// using date_create() function to create
// DateTime object
// using date_format() function to format date
echo date_format($date, "Y/m/d H:i:s");


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.

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:


// PHP program to add 40 days in date
date_add($date, date_interval_create_from_date_string("40 days"));
echo date_format($date, "Y-m-d");





//PHP program to add 1 year, 10 mins, 23 secs in date
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");


2019-12-10 00:10:23

Note : Using ‘+’ operator we can add more to date and time.

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!

Last Updated : 13 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials