PHP Class for Date Time calculations - Part 2
In my previous posting I touched on a PHP class called DateClass (by Steve Powell) that I downloaded from phpclasses.org. The DateClass package actually contained 2 separate classes. One for manipulating dates and the other for date spans.
This post will cover how to use the DateSpanClass to easily calculate the difference between 2 dates, in intervals from seconds to years. Sadly, however, the class have some bugs which needs to be fixed before it can be used.
Fixing the bugs
At Line 709: return $this->_seconds(); Change to: return $this->_hours;
At Line 715: return $this->_minutes(); Change to: return $this->_minutes;
At Lines 817, 855, 895, 936: $dtStart = new DateClass($this->StartDate); Change to: $dtStart = new DateClass($this->StartDate->ToString());
At Lines 823, 861, 901, 941: $dtStart = new DateClass($this->StopDate); Change to: $dtStart = new DateClass($this->StopDate->ToString());
The problem was with the DateClass function called within the DateSpanClass. DateClass was expecting a string as a parameter, but instead it was passed a DateClass object, so by appending ‘->ToString()’, returns a string representation of the DateClass object.
Now the fun starts!
Now that the problem has been fixed, we can get down to some simple examples of how to use the DateSpanClass.
The class allows us to determine the span between two dates. The span can be a wide variety of different kinds of intervals:
- Years
- Quarters
- Months
- Weeks
- Weekdays
- Days
- Hours
- Minutes
- Seconds
Example 1: How many <blahs> are there between now and 54 days later?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?php include ('dateclass.php'); // initiate the DateClass class $dc = new DateClass(); $today = $dc->ToString(); // add an arbitary 54 days to today's date $nd = $dc->Add('days',54); $end_date = $nd->ToString(); // initiate a DateSpanClass $ds = new DateSpanClass(); // will return the number of years (as a float) between $today and $end_date print("Years: ".$ds->Years($today,$end_date)); print("<br>"); // will return the number of quarters (as a float) between the two dates print("Quarters: ".$ds->Quarters($today,$end_date)); print("<br>"); // will return the number of months print("Months: ".$ds->Months($today,$end_date)); print("<br>"); // will return the number of weeks print("Weeks: ".$ds->Weeks($today,$end_date)); print("<br>"); // will return the number of weekdays print("Weekdays: ".$ds->Weekdays($today,$end_date)); print("<br>"); // will return the number of days print("Days: ".$ds->Days($today,$end_date)); print("<br>"); // will return the number of hours print("Hours: ".$ds->Hours($today,$end_date)); print("<br>"); // will return the number of minutes print("Minutes: ".$ds->Minutes($today,$end_date)); print("<br>"); // will return the number of seconds print("Seconds:".$ds->Seconds($today,$end_date)); print("<br>"); ?> |
Simple right? Here’s another example:
Example 2: Return an array of all dates between the 2 given dates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php function getDaysSpan($start,$end) { //initiate the class $span = new DateSpanClass($start,$end); //get the number of days between the 2 dates $daysSpan = $span->Days(); // get the date class for the start date $ds = $span->StartDate; // add it as the first item of the results array $results[] = $ds->ToString('Y-m-d'); // look thru the number of days for ($i=1;$i<=$daysSpan;$i++) { // add one day $ds->Add("d",1); // load the new date into the results array $results[] = $ds->ToString('Y-m-d'); } //return the array return $results; } ?> |
Well, that’s it. This class has made my life easier in doing date calculations, so I hope it will be for you too.
- PHP Class for Date Time calculations - Part 1
- A look at the Google Graph class
- Cacti: Ubuntu 8.04 Cacti Plugin - Invalid PHP_SELF Path problem
- Checking a string is alphanumeric in PHP
- CakePHP: Using scaffolding for rapid application building





