An Overview of the DateTime object in PHP

Most if not all PHP developers are aware of the getdate function and how it returns an associated array of the current time and date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$current_time= getdate();
print_r( $current_time );
$current_time= getdate(); print_r( $current_time );
$current_time= getdate();
print_r( $current_time );

Output Format:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)
Array ( [seconds] => 40 [minutes] => 58 [hours] => 21 [mday] => 17 [wday] => 2 [mon] => 6 [year] => 2003 [yday] => 167 [weekday] => Tuesday [month] => June [0] => 1055901520 )
Array
(
    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520
)

However this function is not very useful when you want perform Date Comparisons and Manipulations. Instead you will want to use the DateTime object of PHP.

The DateTime Class

The DateTime Object by default gets instantiated to the current time when being constructed. If however you want to instantiate it at a different date you can pass in a string representation of the date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$date = new DateTime( ); // same as new DateTime( "now" );
$date2 = new DateTime( '2010-01-01');
$date = new DateTime( ); // same as new DateTime( "now" ); $date2 = new DateTime( '2010-01-01');
$date = new DateTime( ); // same as new DateTime( "now" );
$date2 = new DateTime( '2010-01-01');

It is important to remember that you can use any string in the DateTime constuctor that you would use in the strtotime function. This means you could instantiate the date and time of last Monday, next Friday and etc.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$date = new DateTime( "last Monday" );
$date2 = new DateTime( "next Friday" );
$date = new DateTime( "last Monday" ); $date2 = new DateTime( "next Friday" );
$date = new DateTime( "last Monday" );
$date2 = new DateTime( "next Friday" );

Manipulating Dates

To modify your DateTime objects you can use the add and sub methods that utilize DateInterval objects for representing amounts or you can use the modify method which takes a string as a parameter. I personally find the modify method easier to utilize.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$date = new DateTime();
$date->add( new DateInterval( "P5D" ) );
$date->modify( "+5 days" );
$date = new DateTime(); $date->add( new DateInterval( "P5D" ) ); $date->modify( "+5 days" );
$date = new DateTime();
$date->add( new DateInterval( "P5D" ) );
$date->modify( "+5 days" );

Comparing Date Objects

Comparing Dates is now easy as the DateTime object supports basic equality and comparison functions (==, <, >, etc ). So if you want to check if a date is before another you would simply:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$date = new DateTime( "2003-12-17" );
$date2 = new DateTime( "2005-6-19" );
if( $date < $date2 ) {
// additional logic
}
$date = new DateTime( "2003-12-17" ); $date2 = new DateTime( "2005-6-19" ); if( $date < $date2 ) { // additional logic }
$date = new DateTime( "2003-12-17" );
$date2 = new DateTime( "2005-6-19" );
if( $date < $date2 ) {
  // additional logic
}

Resources

// PHP //

Comments & Questions

Add Your Comment