Datetime Difference in PHP
Date and Time in PHP with Security Considerations - Part 4
Foreword: In this part of the series I talk about Datetime Difference in PHP.
By: Chrysanthus Date Published: 19 Jan 2019
Introduction
When you subtract two datetimes, you have an interval.
The date_create() Function
In simple terms, the syntax is:
date_create([ string $time = "now" ])
The time is optional. When omitted, the current time is considered. I suggest you use the datetime format: yyyy-mm-dd hh:ii:ss e.g 2012-07-14 18:45:31 . You can omit the hh:ii:ss part.
This function returns a new DateTime instance or FALSE on failure. .
The DateInterval Class Synopsis
It is,
DateInterval {
/* Properties */
public integer $y ;
public integer $m ;
public integer $d ;
public integer $h ;
public integer $i ;
public integer $s ;
public float $f ;
public integer $invert ;
public mixed $days ;
/* Methods */
public __construct ( string $interval_spec )
public static DateInterval createFromDateString ( string $time )
public string format ( string $format )
}
Properties
y
Number of years.
m
Number of months.
d
Number of days.
h
Number of hours.
i
Number of minutes.
s
Number of seconds.
f
Number of microseconds, as a fraction of a second.
invert
Is 1 if the interval represents a negative time period and 0 otherwise. See DateInterval::format().
days
If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.
The date_diff() Function
The simple syntax is:
DateInterval date_diff ( DateTimeInterface $datetime1 , DateTimeInterface $datetime2)
where $datetime1 and $datetime2 are objects.
Read and test the following code:
<?php
$datetime1 = date_create('2009-10-28 00:00:00');
$datetime2 = date_create('2012-07-14 18:45:31');
$interval = date_diff($datetime1, $datetime2);
echo 'No of years: ', $interval->y, '<br>';
echo 'No of months: ', $interval->m, '<br>';
echo 'No of days: ', $interval->d, '<br>';
echo 'No of hours: ', $interval->h, '<br>';
echo 'No of minutes: ', $interval->i, '<br>';
echo 'No of seconds: ', $interval->s, '<br>';
echo '0 if interval is +ve or 1 otherwise: ', $interval->invert, '<br>';
?>
The output is:
No of years: 2
No of months: 8
No of days: 16
No of hours: 18
No of minutes: 45
No of seconds: 31
0 if interval is +ve or 1 otherwise: 0
Adding and Subtracting Durations in PHP Datetime
Spellings of durations that PHP datetime functions would accept are:
sec, secs, second, seconds
min, mins, minute, minutes
hour, hours
day, days
weeks
fortnight, fortnights, forthnight, forthnights
month, months
year, years
The date_add() Function
This function adds a duration to datetime, and returns a datetime object. The syntax is:
datetimeObj date_add($datetimeObject, durationObject);
Read and test the following code:
<?php
$datetime = date_create('2012-07-14 18:45:31');
date_add($datetime, date_interval_create_from_date_string('10 days'));
echo date_format($datetime, 'Y-m-d H:i:s');
?>
Note the use of the date_interval_create_from_date_string() function that returns a duration object. Also note that use of the date_format() function.
The output is:
2012-07-24 18:45:31
The date_sub() Function
This function subtracts a duration from datetime, and returns a datetime object. The syntax is:
datetimeObj date_sub($datetimeObject, durationObject);
Read and test the following code:
<?php
$datetime = date_create('2012-07-14 18:45:31');
date_sub($datetime, date_interval_create_from_date_string('10 day'));
echo date_format($datetime, 'Y-m-d H:i:s');
?>
Note the use of the date_interval_create_from_date_string() function that returns a duration object. Also note that use of the date_format() function.
The output is:
2012-07-04 18:45:31
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links