We now have a youtube channel. Subscribe!

PHP | date_interval_format() Function

PHP date_interval_format() Function

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_interval_format() Function.

The PHP date_interval_format() function is an alias of the DateInterval::format(). This built-in function accepts an interval and a format string as parameters and, formats the given interval in the specified format.


Following below is the syntax to use this function -

date_interval_format($interval, $format)

Parameter Details

Sr.NoParameter & Description

interval (Mandatory)

This is an object of the DateInterval you need to format.


format (Mandatory)

This is a string value, specifying the format.

Return Value

This PHP function returns the formatted interval.

PHP Version

This function was first introduced as part of core PHP version 5.3 and, it works with all of the later versions.


Following example demonstrates the usage of date_interval_format() function -

  $interval = new DateInterval('P25DP8MP9Y');
  $format = "%d days;
  $res = date_interval_format($interval, $format);


When the above code is executed, it will produce the following result -

25 days


Unlike other date/time functions, the date_interval_format() does not recalculate the carry over points in date and time strings. Therefore if you pass the date and time values beyond their boundaries, they will be formatted as it is -

   $interval = new DateInterval('P45M');
   $format = "%m months";
   $res1 = date_interval_format($interval, $format);
   $res2 = date_interval_format(new DateInterval('PT30H'), "%h hours");


When the above code is executed, it will produce the following result -

45 months
30 hours


Following example below calculates the difference between a given date and the current date and formats the result via date_interval_format() function -

   $date1 = date_create("25-09-1989");
   $date2 = date_create("1-09-2012");
   $interval = date_diff($date1, $date2);
   $res = date_interval_format($interval, '%Y years %d days');



Try the following example -

print(date_interval_format(new DateInterval('P12D'), "%d days")."\n");
print(date_interval_format(new DateInterval('P7M'), "%m months")."\n");
print(date_interval_format(new DateInterval('P12Y'), "%y years")."\n");
print(date_interval_format(new DateInterval('PT9H'), "%h hours")."\n");
print(date_interval_format(new DateInterval('PT45S'), "%s seconds")."\n");


When the above code is executed, it will produce the following result -

22 years 7 days
12 days
7 months
12 years
9 hours
45 seconds

Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be discussing about the date_create_immutable_from_format() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain