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:

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on July 21, 2008 at 6:50 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: , ,

One Response

Subscribe to comments via RSS

  1. Written by Leigh Harrison on April 1, 2009 at 3:09 pm
    Permalink

    Another errata – you need to change line 709 from
    return $this->_hours();
    to
    return $this->_hours;

    My file numbering becomes one line different from yours part-way through (I don’t know why), so 709 may be 708 or 710 – I don’t recall which way the offset went.

    Cheers,

    ::Leigh
    http://www.else.co.nz/

Subscribe to comments via RSS

Leave a Reply