DATE_DIFF()
DATE_DIFF(date, subtract, [unit])
Calculate the difference between 2 dates and returns the result in the specified units (days, hours...).
It is possible to provide only the time part (without date), and then the following rules apply:
If the date part of
date
is not present, assume that it corresponds to the current datetime (in the session timezone).
If the time part of
subtract
is not present, then assume that it corresponds to the same date thandate
, except when the time is bigger than the time of thedate
. In the latter case, consider that the date to subtract is in the previous day.
For example, to calculate the time in bed of a person that went to sleep at 22:00 and woke up the next day at 08:00, we would subtract 08:00 - 22:00, and the result would be 10h (because 22:00 is considered to be in the previous day)
Input parameters
Parameters | Type | Default | Required | Description | Default |
---|---|---|---|---|---|
|
| Yes | Base date: a string specifying a valid datetime (e.g. "2019-11-01 12:00:34") or time (e.g. "23:00"). If the date part is not provided, then the current date (in the session timezone) will be assumed. |
| |
|
| Yes | Value to subtract the date.Two types of value are accepted
|
| |
| "D" | No | Units in which the return value will be expressed:
If no unit is specified, then "d" is the default value (integer number of days) If SUBTRACT is expressed as a number, then this parameter is ignored and the return value will be a date time formatted string |
|
Return value
Type | Description |
---|---|
Returns the difference between the dates in the requested units. Note that decimal values can be returned (e.g. 2.5 days) |
Execution
Always. Calculated on-the-fly whenever an ITEM of a FORM is changed (by means of form_set_answer(), or when the FORM is opened
Examples
Formula | Description |
---|---|
| No UNIT specified, so "D" is assumed. The final result would be 1 (day) |
| No UNIT specified, so "D" is assumed. The difference returns a decimal value. The final result would be 1.5 (day) |
| Return requested in hours. The final result would be 36 (hours) |
| Only the time part is provided. The calculation will consider that the date to subtract belongs to the previous day because the time to subtract is bigger that the base time |
| Subtract two dates and return the result formatted as a time string. The result would be "36:00:00" |
| Legacy support. Subtracts 3 days from the date and returns a string with the final date. The result would be "2019-05-09". You should use DATE_ADD() instead of DATE_DIFF() to perform this kind of calculations |