It is just too normal to manipulate date and time when you are programming. When you are working on major projects, you will find that you need to manipulate either date or time, or in some cases both, just to make sure the program is well formed.
PHP has in store a wide range of tools that can make manipulating data and powering up the web apps easy and convenient. Here you will be introduced to the PHP data interval class that is particularly used for mapping dates within a web app. The whole schedule is to take you through data interval class and how it is used in PHP.
Introduction to Interval
Before moving on to understand what a date interval class is, it is very important for you to understand what an interval is. Interval is the duration of time. Let’s simplify this for you. When you tell someone “I will be back in 10 minutes” or “I will do this in 10 minutes”, you are actually specifying an interval. How will your web app understand this interval of 10 minutes? An international standard ISO 8601 comes to aid when you need to use, store or transfer the dates, time duration or even time for that matter.
The string used for this purpose would be:
- P: period
- Y: years
- M: months
- D: days
- T: time
- H: hours
- M: minutes
- S: seconds
P in the whole string is mandatory but the other elements are optional.
Examples: P2Y (Period 2 years)
PT3s (Period Time 3 seconds)
How to Create a Date interval Object?
Now that you are familiar with interval, let’s move to creating a date interval object. For this we will need to mull around PHP date interval class a bit. For this you need to consider a more obtainable time duration as the target. A period of 1 year, 2 months, 3 days 4 hours, 5 minutes and 6 seconds has been considered as the output of this class.
$Duration=new DateInterval (“P1Y2M3DT4J5M6s”);
The result you would get is
Date interval Object
The whole date interval class has managed to store the individual values in its properties and then the duration string was passed into the result.
Interesting this function is! Now that you know how to use it, the next question that may bother you is how to use it, or what benefits this function might have for you. It benefits you when as a developer you are planning to indulge in manipulations.
Manipulating Dates & Durations
Here’s where you put the date interval class to use. When you are working with date interval object, there are three methods at your disposal. One is to add, second is to subtract and third is the diff method.
When you use add and sub as the tools, they will plainly modify your date time object and turn it into date interval object. In case of diff, it will compare the two date time objects and return the date interval object as a result.
You will find that all three methods are extremely useful, but in case of diff you will get total days as your result too which becomes valuable piece of information for you.
Extracting Intervals from Durations
As a developer there are chances you may want to retrieve the stored pieces of information. This is where date interval class of PHP comes as a benefit. With the table for format method, you can always use date interval class to retrieve relevant information.
It is important to note that while date interval duration represents total days in the %a format, in the strftime format it is represented in the textual format.
With date interval, you don’t have an internal option present to retrieve information without returning zero values. In this case you can apply two different methods: extracting with 0 elements and extending the date interval class.
To extend using the 0 class you can use the earlier method, while for new date interval you will need to use the below mentioned code
$interval_spec = ‘P345D’;$Duration1 = new DateInterval( $interval_spec );echo $Duration1->format(‘%d’); //
345 $interval_spec = $Duration1->format(‘P%yY%mM%dDT%hH%iM%sS’);
// P0Y0M345DT0H0M0S$Duration2 = new DateInterval( $interval_spec );echo $Duration2->format(‘%d’);
This is how date interval class can be useful and beneficial to PHP developers in web apps.