Methods
date->diff()
Calculate the chronological difference between two NepaliDate instances in specific time units.
The diff method calculates the elapsed time duration between the current NepaliDate instance and another date object. It can return the difference in days, months, years, hours, minutes, seconds, or milliseconds, making it suitable for calculating age, tenure, or interval durations.
Import
use NepaliDateLibrary\NepaliDate;Signature
public function diff(NepaliDate $date, string $unit): int;Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$date | NepaliDate | Yes | Target date instance to compare against. |
$unit | string | Yes | Measurement unit: 'days', 'months', 'years', 'hours', 'minutes', 'seconds'. |
Return Value
- Type:
int - Description: Integer difference count in the specified unit.
Example
use NepaliDateLibrary\NepaliDate;
$d1 = new NepaliDate('2081-01-01');
$d2 = new NepaliDate('2081-01-20');
$daysDiff = $d2->diff($d1, 'days');
echo "Difference in days: {$daysDiff}"; // 19Related
date->isBefore(): Check chronological order.date->isAfter(): Check chronological order.
Advertisement
date->addYears()
Add or subtract years to a NepaliDate instance while preserving month and day values.
date->startOfDay()
Return a new NepaliDate instance set to the first moment of the current day at midnight.