- DateInterval::format
- Example
- Questions
- The DateInterval class
- Class synopsis
- Properties
- Changelog
- Table of Contents
- Date_interval_format()
- What is the date_interval_format() function?
- Syntax
- Examples
- Example 1:
- Example 2:
- Formats
- Conclusion
- The DateInterval Class
- Adding and Subtracting Dates and Times
- DateInterval::createFromDateString
- DateInterval::format
- PHP date_interval_format() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
DateInterval::format
$date1=new DateTime('2019-05-30'); $date2=new DateTime('2013-02-01'); $difference=$date1->diff($date2); echo $difference->format('%Y Years , %M Months, %D Days : In total %a days');
06 Years , 03 Months, 29 Days : In total 2309 days
Example
$interval=new DateInterval('P5Y4M2DT5H3M4S'); echo "%Y Years : ".$interval->format('%Y Years')."
"; echo "%y Years : ".$interval->format('%y Years')."
"; echo "%M Months : ".$interval->format('%M Months')."
"; echo "%m Months : ".$interval->format('%m Months')."
"; echo "%D Days : ".$interval->format('%D Days')."
"; echo "%d Days : ".$interval->format('%d Days')."
"; echo "%H Hours : ".$interval->format('%H Hours')."
"; echo "%h Hours : ".$interval->format('%h Hours')."
"; echo "%I Minutes : ".$interval->format('%I Minutes')."
"; echo "%i MInutes : ".$interval->format('%i Minutes')."
"; echo "%S Seconds : ".$interval->format('%S Seconds')."
"; echo "%s Seconds : ".$interval->format('%s Seconds')."
"; //echo "%F Microseconds : ".$interval->format('%F Microseconds')."
"; // PHP 7 //echo "%f Microseconds : ".$interval->format('%f Microseconds')."
"; // PHP 7
%Y Years : 05 Years %y Years : 5 Years %M Months : 04 Months %m Months : 4 Months %D Days : 02 Days %d Days : 2 Days %H Hours : 05 Hours %h Hours : 5 Hours %I Minutes : 03 Minutes %i Minutes : 3 Minutes %S Seconds : 04 Seconds %s Seconds : 4 Seconds %F Microseconds : 000000 Microseconds %f Microseconds : 0 Microseconds
Questions
- What is the syntax for creating a DateInterval object in PHP?
- How can you represent a DateInterval of 5 days, 2 hours, and 30 minutes in PHP?
- What are the available format specifiers for the DateInterval constructor?
- How can you add a DateInterval to a specific DateTime object in PHP?
- How do you format a DateInterval object to display only the days, hours, and minutes?
- How can you calculate the total number of hours in a DateInterval object in PHP?
- What is the difference between the % sign and the P sign in a DateInterval format specifier?
- Can you specify negative values in a DateInterval object? If yes, how?
- How can you convert a DateInterval object into a string representation in PHP?
- Are there any limitations or restrictions on the values that can be used in a DateInterval object?
plus2net.com
The DateInterval class
A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTimeImmutable ‘s and DateTime ‘s constructors support.
More specifically, the information in an object of the DateInterval class is an instruction to get from one date/time to another date/time. This process is not always reversible.
A common way to create a DateInterval object is by calculating the difference between two date/time objects through DateTimeInterface::diff() .
Since there is no well defined way to compare date intervals, DateInterval instances are incomparable.
Class synopsis
Properties
The available properties listed below depend on PHP version, and should be considered as readonly.
Number of microseconds, as a fraction of a second.
Is 1 if the interval represents a negative time period and 0 otherwise. See DateInterval::format() .
If the DateInterval object was created by DateTimeImmutable::diff() or DateTime::diff() , then this is the total number of full days between the start and end dates. Otherwise, days will be false .
If the DateInterval object was created by DateInterval::createFromDateString() , then this property’s value will be true , and the date_string property will be populated. Otherwise, the value will be false , and the y to f , invert , and days properties will be populated.
Changelog
Version | Description |
---|---|
8.2.0 | The from_string and date_string properties were added for DateInterval instances that were created using the DateInterval::createFromDateString() method. |
8.2.0 | Only the y to f , invert , and days will be visible. |
7.4.0 | DateInterval instances are incomparable now; previously, all DateInterval instances were considered equal. |
7.1.0 | The f property was added. |
Table of Contents
- DateInterval::__construct — Creates a new DateInterval object
- DateInterval::createFromDateString — Sets up a DateInterval from the relative parts of the string
- DateInterval::format — Formats the interval
Date_interval_format()
In this article, we will discuss the PHP function date_interval_format() and its usage in date and time calculations. We will explore the syntax, examples, and different formats that can be used with this function. Our goal is to provide you with a clear understanding of how to use this function in your PHP code.
What is the date_interval_format() function?
The date_interval_format() function is a PHP built-in function used to format a date interval. It is used to calculate the difference between two dates and format the result according to a specified format. The function returns a string that represents the formatted date interval.
Syntax
The syntax for the date_interval_format() function is as follows:
date_interval_format($interval_object, $format_string);
Here, $interval_object is a DateInterval object, and $format_string is the format string used to format the date interval.
Examples
Let’s take a look at some examples to understand how the date_interval_format() function works.
Example 1:
$datetime1 = new DateTime('2022-03-03 00:00:00'); $datetime2 = new DateTime('2023-03-03 00:00:00'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days');
In this example, we are calculating the difference between two dates and formatting the result using the %R%a format string, which displays the number of days between the two dates.
Example 2:
$datetime1 = new DateTime('2022-03-03 00:00:00'); $datetime2 = new DateTime('2023-03-03 00:00:00'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days %H hours %I minutes');
+365 days 00 hours 00 minutes
In this example, we are calculating the difference between two dates and formatting the result using the %R%a days %H hours %I minutes format string, which displays the number of days, hours, and minutes between the two dates.
Formats
The date_interval_format() function supports various format strings that can be used to format the date interval. Here are some of the commonly used format strings:
- %a — Number of days between the two dates.
- %h — Number of hours between the two dates.
- %i — Number of minutes between the two dates.
- %s — Number of seconds between the two dates.
- %y — Number of years between the two dates.
- %m — Number of months between the two dates.
- %d — Number of days between the two dates excluding months and years.
- %R — Displays the sign of the result. Returns ‘+’ for positive intervals and ‘-‘ for negative intervals.
Conclusion
In conclusion, the date_interval_format() function is a useful tool for calculating the difference between two dates and formatting the result according to a specified format. By using the function and the different format strings, you can display the date interval in various formats that suit your needs. We hope this article has been informative and helpful in your PHP programming endeavors.
graph LR A[Start] --> B(Date1) A --> C(Date2)
The DateInterval Class
How to add or subtract a date/time period from a DateTime object using the add() and sub() methods.
Adding and Subtracting Dates and Times
The DateTime class contains add() and sub() methods to manipulate a DateTime instance’s value. Both methods accept a DateInterval class instance as the argument that specifies the amount of time added to or subtracted from a DateTime instance.
- P – The format must start with the letter P (period), the following designators represent the period:
- Y (years): P2Y means 2 years.
- M (months): P2Y3M means 2 years and 3 months.
- D (days): P2Y3M29D means 2 years, 3 months, and 29 days.
- W (weeks): P2Y3M2W means 2 years, 3 months, and 2 weeks.
- H (hours): PT2H means 2 hours, P2Y3M29DT2H means 2 years, 3 months, 29 days, and 2 hours.
- M (minutes): PT2H5M means 2 hours and 5 minutes. P2YT2H5M means 2 years, 2 hours, and 5 minutes.
- S (seconds): PT59S means 59 seconds only. P3MT5S means 3 months, and 5 seconds.
The interval specification can also be represented as a date time. For example, the P2YT2H5M format can be written as P0002-00-00T02:05:00 .
Example: Adding two weeks to the DateTime object
format('d-M-Y H:i:s') . '
'; # 01-Jan-2022 14:00:00 $date->add($interval); echo $date->format('d-M-Y H:i:s') . '
'; # 15-Jan-2022 14:00:00 // Adding the same interval again $date->add($interval); echo $date->format('d-M-Y H:i:s'); # 29-Jan-2022 14:00:00Example: Subtracting two weeks to the DateTime object
format('d-M-Y H:i:s') . '
'; # 01-Jan-2022 14:00:00 $date->sub($interval); echo $date->format('d-M-Y H:i:s') . '
'; # 18-Dec-2021 14:00:00 // Subtracting the same interval again $date->sub($interval); echo $date->format('d-M-Y H:i:s'); # 04-Dec-2021 14:00:00DateInterval::createFromDateString
Alternatively, you can use the static createFromDateString() method, which takes as an argument an English relative date string in the same way as strtotime() function does. So, the P2W is equal to “ 2 weeks “, P1D is equal to “ 1 day “, PT36S is equal to “ 36 seconds “, etc. see the following code:
$interval = DateInterval::createFromDateString('2 weeks'); $date = new DateTime('Jan 01, 2022 2:00:00PM'); echo $date->format('d-M-Y H:i:s') . '
'; # 01-Jan-2022 14:00:00 $date->sub($interval); echo $date->format('d-M-Y H:i:s') . '
'; # 18-Dec-2021 14:00:00 // Subtracting the same interval again $date->add($interval); echo $date->format('d-M-Y H:i:s'); # 01-Jan-2022 14:00:00DateInterval::format
- %Y (years): 01, 08, 22
- %y (years): 1, 8, 22 (without leading zero)
- %M (months): 01, 03, 12
- %m (months): 1, 3, 12 (without leading zero)
- %D (days): 01, 08, 31
- %d (days): 1, 8, 31 (without leading zero)
- %a Total number of days as a result of a DateTime::diff()
- %H (hours): 01, 07, 22
- %h (hours): 1, 7, 22 (without leading zero)
- %I (minutes): 01, 06, 59
- %i (minutes), 1, 6, 59 (without leading zero)
- %S (seconds): 01, 03, 55
- %s (seconds): 1, 3, 55 (without leading zero)
- %F (microseconds), at least 6 digits; 000701, 005273, 424288
- %f (microseconds),701, 5273, 424288 (without leading zero)
- %R + or – sign for positive and negative value
- %r – sign when negative, empty when positive
Example: Formatting interval
format('%r%d days'). '
'; # Prints: -10 days $interval = new DateInterval('P1DT1H'); echo $interval->format('%R%D days and %R%h hours'); # Prints: +01 days and +1 hoursThe Date and Time Tutorials:
PHP date_interval_format() Function
Calculate the interval between two dates, then format the interval:
// %a outputs the total number of days
echo $diff->format(«Total number of days: %a.»);
?>Definition and Usage
The date_interval_format() function is an alias of DateInterval::format().
The DateInterval::format() function is used to format the interval.
Syntax
Parameter Values
- % — Literal %
- Y — Year, at least 2 digits with leading zero (e.g 03)
- y — Year (e.g 3)
- M — Month, with leading zero (e.g 06)
- m — Month (e.g 6)
- D — Day, with leading zero (e.g 09)
- d — Day (e.g 9)
- a — Total number of days as a result of date_diff()
- H — Hours, with leading zero (e.g 08, 23)
- h — Hours (e.g 8, 23)
- I — Minutes, with leading zero (e.g 08, 23)
- i — Minutes (e.g 8, 23)
- S — Seconds, with leading zero (e.g 08, 23)
- s — Seconds (e.g 8, 23)
- F — Microseconds, at least 6 digits (e.g 004403, 235689)
- f — Microseconds (e.g 4403, 235689)
- R — Sign «-» when negative, «+» when positive
- r — Sign «-» when negative, empty when positive
Note: Each format character must be prefixed by a % sign!
Technical Details
Return Value: Returns the formatted interval PHP Version: 5.3+ PHP Changelog: PHP 7.1: Added the F and f parameters ❮ PHP Date/Time Reference
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.