in Object-Oriented Development

Cool DateTime Functions In PHP 5.3

Over time, the PHP DateTime object has become one of the best objects available to PHP developers. This object has grown since early PHP 5 into a robust class that has the ability to do lots of great things.

Recently, I was exploring some of the functionality provided by the DateTime object as of PHP 5.3 (and wishing that Ubuntu had PHP 5.3 as a package distribution). Here are some of the new things in PHP 5.3 that are really cool.

Note: you can read the manual on the DateTime object here.

DateTime::add() and DateTime::sub()
The add() and sub() methods are about adding or subtracting the number of days, months, years, etc. from a DateTime object. The interface is a bit clunky, requiring you to pass in a DateInterval object. However, this still provides an easy way to modify a DateTime object.

For example, let’s say we wanted to add 3 weeks to our DateTime object:


$dt = new DateTime(); // Set to now.
$dt->add(new DateInterval('PW3'));
echo $dt->format('n/j/Y'); // Outputs 3 weeks from today's date.


How is this an improvement over using the DateTime::modify() method? It improves on it in one specific way: it’s object-oriented. Rather than passing a string you have the ability to pass an object.

One of the coolest PHP 5.3 features introduced was the ability to diff two DateTime objects. This returns to you a DateInterval object, which contains the details of how different the objects are.

$dt1 = new DateTime('August 3rd, 2004');
$dt2 = new DateTime('August 10th, 2006');

The result that you get looks like this:

public ‘y’ => int 2
public ‘m’ => int 0
public ‘d’ => int 7
public ‘h’ => int 0
public ‘i’ => int 0
public ‘s’ => int 0
public ‘invert’ => int 0
public ‘days’ => int 737

This can be extremely useful in determining the time difference between two objects.

DateTime::getTimestamp() and DateTime::setTimestamp()
Sometimes it’s just useful to be able to grab the Unix timestamp from the DateTime object. But prior to PHP 5.3, to do so required some clunky code using strtotime() and a formatted date string. PHP has fixed this, and you can now use these getter and setter methods to get the Unix timestamp.

Be the first to get Modern Object-Oriented PHP!

Long to learn how you can develop modern applications using object-oriented PHP? Curious about how to apply all these best practices to your code?

Modern Object-Oriented PHP is a brand-new book focused on teaching you the techniques you need for writing modern, well-designed object-oriented applications!

The book lands in April. Sign up today for a sample chapter plus special launch day discounts!

Powered by ConvertKit


  1. For some (perverse?) reason I feel I had to wrap Date around my own classes to utilise the new functionality… maybe I just favour my own API?

  2. Another fine DateTime feature is native comparing:

    $dateStart = new DateTime('yesterday');
    $dateEnd = new DateTime('tomorrow');
    if ($dateEnd

  3. Yes, I started using DateTime recently because of its representation being better than using date strings, and it’s ability to represent a much larger date range than unix timestamps.

    On the Zend Framework contributors mailing list, there’s a discussion at the moment of the possibility of scrapping Zend_Date or extending DateTime sometime in the future. Zend_Date uses mktime internally, so not as good.

    Before PHP 5.3, you are able to run “$date->format(‘U’)” and “new DateTime(‘@’.$timestamp)”, rather than having to run the functions you mentioned.

    Your diff example appears to be missing some code.

  4. I like using this to display the run time of a script (especially those long ones that might take 30 minutes to run).

    What’s great about using an object is that you have a lot of built in functionality that you would have to otherwise build in yourself.

  5. date(‘U’); // <— this works great if you want the current unix timestamp

Comments are closed.