PHP Class for Date Time calculations - Part 1

Performing data/time calculation is basic requirement in any programming project. No serious application can hide from it, so thankfully PHP itself comes with some very useful data/time functions. But, I feel that those PHP date/time functions are probably great for data/time display and formatting, but not so much for calculating for things like the beginning/end of the week, the number of seconds between two dates etc.

Again thankfully, after a visit to phpclasses.org, I found a nifty little class written by Steve Powell (way back in 2004) called DateClass which did what I wanted. Unfortunately, the documentation was only a class reference sheet, and doesn’t come with any examples to quickly get things going.

The DateClass package actually contains 2 individual classes. One for manipulating dates and the other for manipulating date spans. This post will only cover the actual DateClass object. I’ll post the followup in Part 2 for the DateSpanClass object soon.

Let’s get cracking.

Example 1: Display the current date and time.

1
2
3
4
5
6
7
8
9
10
<?php
// include the DateClass class
include('dateclass.php');
 
// initiate the class
$dc = new DateClass();
 
// output to string
echo $dc->ToString();
?>

Yes, I know, this does the same as calling date()function in PHP.

1
echo date('Y-m-d H:i:s');

So, why should you use such a long winded method? Well, it’s the other methods in the DateClass class that we’re interested in.

The class comes with the standard calls you’ll find equivalent from PHP, such as, Year(), Month(), Day(), Hours(), Minutes(), Seconds(), Timestamp(), etc. So they’re all pretty self explanatory. Instead, let’s look at some of the more interesting methods.

Example 2: Find the beginning and end of the week.

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
<?php
// include the DateClass class
include('dateclass.php');
 
// initiate the class
$dc = new DateClass();
 
// calling BOW() will return another DateClass object
// for beginning of the week.
$bow = $dc->BOW();
 
// output the datetime for the beginning of this week.
echo $bow->ToString();
 
// calling EOW() will return me another DateClass object 
// for end of the week
$eow = $dc->EOW();
 
// output the datetime
echo $eow->ToString();
 
// destroy the DateClass objects
$dc = "";
$bow = "";
$eow = "";
?>

I think you get the idea. The class also includes methods for:

  • BOM() and EOM() - Beginning and end of the month
  • BOQ() and EOQ() - Beginning and end of the quarter
  • BOY() and EOY() - Beginning and end of the year
  • Quarter() - which returns the calendar quarter (1-4) the current date value of class is in.

Example 3: Calculating dates 2 days forward and then 2 months back
Lastly, let’s look at the date calculation using the Add() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include ('dateclass.php');
 
// initiate the class
$dc = new DateClass();
 
// add 2 days to the current date
$nd = $dc->Add('days',2);
 
// subtract 2 months to the previous calculated date
$nd = $dc->Add('months',-2);
 
// output the new datetime
echo $nd->ToString();
?>

Although using DateClass is a bit more verbose than say, using strtotime() approach, I find this more intuitive and easier to understand. But that’s just me.

Coming up in my next post, I’ll cover what the DateSpanClass can do, and how to return results like the number of whatever (days, months, hours, even weekdays) between 2 dates. Watch out for it.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
Other similar posts:


3 Responses to “PHP Class for Date Time calculations - Part 1”

  1. Milos () Says:

    A little off-topic question:
    Can you tell me (or us :) ) the secret behind the code syntax highlighting and line numbering in your posts? :)

    Thanks,
    Milos

  2. webmaster () Says:

    Hi Milos

    It’s no secret :) I’m using the WP-Syntax plugin for wordpress.

    http://wordpress.org/extend/plugins/wp-syntax/

  3. Milos () Says:

    Thanks!

    Well, i knew it is no secret, but i didn’t realize that the answer so obvious :D :-[

    Hehe, thanks again :)

Leave a Reply

Google