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:

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:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on July 14, 2008 at 6:34 pm by Eldee · Permalink
In: PHP Tutorials · Tagged with: 

5 Responses

Subscribe to comments via RSS

  1. Written by Milos on July 16, 2008 at 6:01 pm
    Permalink

    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. Written by webmaster on July 21, 2008 at 2:09 pm
    Permalink

    Hi Milos

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

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

  3. Written by Milos on July 22, 2008 at 9:32 pm
    Permalink

    Thanks!

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

    Hehe, thanks again :)

  4. Written by Marco on November 14, 2008 at 5:10 pm
    Permalink

    Thanks, thanks, thanks.

    Really useful!

  5. Written by Natalia on May 26, 2009 at 5:50 am
    Permalink

    Hi,

    I need to get the date for the beginning of the week. I’m using the BOW() function. However the week according to code starts on Sunday, while I need it to start on Monday. Please help.

    Thanks,
    Natalia

Subscribe to comments via RSS

Leave a Reply