Date::getDiff
Get the difference between two dates
Description
public number Date::getDiff( string $fromDateTime, string $toDateTime, [ boolean $month = false ] )
Returns $toDateTime minus $fromDateTime in days. The result is a decimal number when the times of day differ (12 hours is 0.5). With $month, the difference is counted in calendar months, ignoring the days (2024-01-31 to 2024-02-01 is 1).
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $fromDateTime | string | Start date and time | |
| $toDateTime | string | End date and time | |
| $month | boolean | false | true: in months / false: in days |
Example
Controller
use Fegg\Tool\Date;
$date = new Date();
echo $date->getDiff('2024-04-01', '2024-04-10'); // -> 9
echo $date->getDiff('2024-04-10', '2024-04-01'); // -> -9
echo $date->getDiff('2024-01-31', '2024-03-01', true); // -> 2