In my previous posting I touched on a PHP class called DateClass (by Steve Powell) that I downloaded from 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?


// 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));
"); // will return the number of quarters (as a float) between the two dates print("Quarters: ".$ds->Quarters($today,$end_date)); print("
"); // will return the number of months print("Months: ".$ds->Months($today,$end_date)); print("
"); // will return the number of weeks print("Weeks: ".$ds->Weeks($today,$end_date)); print("
"); // will return the number of weekdays print("Weekdays: ".$ds->Weekdays($today,$end_date)); print("
"); // will return the number of days print("Days: ".$ds->Days($today,$end_date)); print("
"); // will return the number of hours print("Hours: ".$ds->Hours($today,$end_date)); print("
"); // will return the number of minutes print("Minutes: ".$ds->Minutes($today,$end_date)); print("
"); // will return the number of seconds print("Seconds:".$ds->Seconds($today,$end_date)); print("
"); ?>

Simple right? Here’s another example:

Example 2: Return an array of all dates between the 2 given dates

	// 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
		// 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.