An Introduction to the PHP DateInterval Class - Six Revisions |
An Introduction to the PHP DateInterval Class Posted: 24 Jun 2014 03:00 AM PDT Date and time manipulation is an unavoidable part of programming; there will inevitably be a part of a project that requires a date to be modified. While, on the surface, echoing a date in the future brings a small bit of accomplishment, it can be quickly eradicated when one is tasked with manipulating that date.
Without being aware of the arsenal of tools PHP provides you with regards to working with dates, you might find yourself writing kludges that convert strings into timestamps for various comparisons. Today we’re going to dissect the PHP DateInterval class, a power tool for dealing with dates in a web app. But before learning about the What is an Interval?Simply put, an interval is a duration of time. When we converse about the topic of duration, we wouldn’t say, "Honey, I’ll be home on Dec 3rd, 2015 15:19pm". Instead, we would simplify and say "I’ll be home in 5 minutes". Although this is ideal when conversing with another person, this is not so great as a data construct. So how do we define "in 5 minutes" for a web app? Fortunately, there’s an ISO for that. ISO 8601 is the international standard that defines how to use, store, and transfer date, time, and duration information. The specific string to define durations is an easy-to-remember pattern: PYMDTHMS
Except for Immediately preceding each designator is the quantity of time you want to store. Below is a table showing samples of different date and time ranges:
Creating a DateInterval ObjectNow that we understand what an interval is — it’s simply a duration of time — and that ISO 8601 defines our duration’s format, we can start playing around with the PHP Let’s define a predictable duration, create a We’ll start with "1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds". $Duration = new DateInterval( "P1Y2M3DT4H5M6S" ); print_r( $Duration ); Result: DateInterval Object ( [y] => 1 [m] => 2 [d] => 3 [h] => 4 [i] => 5 [s] => 6 ... ) You will notice that the constructor for the Let’s try another more practical duration: 1 month. You’ll notice that any undefined durations are automatically defaulted to 0. $Duration = new DateInterval( "P1M" ); print_r( $Duration ); Result: DateInterval Object ( [y] => 0 [m] => 1 [d] => 0 [h] => 0 [i] => 0 [s] => 0 ... ) So now we know what’s happening, but what do we really have? What’s so great and tangible about this? The real benefit to what we have here is that the Note: It’s important to note that creating a That makes sense because we don’t have a source or destination date. Imagine the case of 61 days:
Manipulating Dates with DurationsDurations tame the manipulation of PHP DateTime objects. The PHP
Both $Now = new DateTime(); echo $Now->format('Y-m-d'); // 2014-06-12 $Duration = new DateInterval('P1M'); // 1 month $Now->add( $Duration ); echo $Now->format('Y-m-d'); // 2014-07-12 $Duration = new DateInterval('P2M'); // 2 months $Now->sub( $Duration ); echo $Now->format('Y-m-d'); // 2014-05-12 In the above example you can see how date manipulation becomes much easier and more predictable by using The $Year = new DateInterval('P1Y'); echo $Year->days; // 0 $Date1 = new DateTime(); $Date2 = new DateTime(); $Date2->add( $Year ); $Difference = $Date1->diff( $Date2 ); echo $Difference->days; // 365 Reading Durations and Extracting IntervalsThere are inevitably going to be times where you want to extract portions of the stored interval (much the same as you would a date). The You use the $Duration = new DateInterval('P345D'); echo $Duration->format('I am %d days'); // I am 345 days Note: It’s important to note, for those familiar with the Oddly enough,
The second option, extending the To extract the interval spec with the 0 elements intact, you can use the same format method like so: $Duration = new DateInterval('P345D'); echo $Duration->format('P%yY%mM%dDT%hH%iM%sS'); // P0Y0M345DT0H0M0S If you want to confirm the above, simply create a new $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'); // 345 You should now have a grasp on using the PHP Hopefully you’re able to apply this current and future web app projects. Related Content
About the AuthorThe post An Introduction to the PHP DateInterval Class appeared first on Six Revisions. |
You are subscribed to email updates from Six Revisions To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No comments:
Post a Comment