Date difference
Last updated on
30 April 2025
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
The syntax to create a new date object is:
$date = new DateObject($value, date_default_timezone(), DATE_FORMAT_ISO);
The following code sample shows how to calculate the difference in days between today and the value in a node's date field that was saved in ISO format:
$date_value = field_get_items('node', $node, 'field_date')[0]['value'];
$fieldDate = new DateObject($date_value, date_default_timezone(), DATE_FORMAT_ISO);
$nowDate = date_now();
$diff = $fieldDate->difference($nowDate, 'days');
See DateObject::difference() for more information on that method.
If your date is in a default Date field, you would need to use DATE_FORMAT_DATETIME instead of DATE_FORMAT_ISO.
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion