NepaliDate Class
The NepaliDate class is the core of the library, providing a complete API for creating, manipulating, and formatting Nepali (Bikram Sambat) dates.
Import
use NepaliDateLibrary\NepaliDate;Timezone Handling
Unlike the NodeJS port (which reads hour/minute/second from the host's local timezone), the PHP port computes getDay(), getHours(), getMinutes(), getSeconds(), and day/week/month/year boundaries in UTC. This makes results deterministic regardless of the server's configured timezone.
Constructors
The NepaliDate constructor accepts several different argument shapes:
Default Constructor
Creates a NepaliDate for the current date and time.
new NepaliDate()Example:
$today = new NepaliDate();
echo $today->toString(), PHP_EOL; // '2082/10/1'From a DateTimeInterface
Creates a NepaliDate from a DateTime or DateTimeImmutable object.
new NepaliDate(DateTimeInterface $date)Parameters:
$date- ADateTimeorDateTimeImmutableinstance
Example:
$phpDate = new DateTimeImmutable('2026-01-14', new DateTimeZone('UTC'));
$nepaliDate = new NepaliDate($phpDate);
echo $nepaliDate->toString(), PHP_EOL; // '2082/10/1'From NepaliDate
Creates a copy of another NepaliDate instance.
new NepaliDate(NepaliDate $date)Example:
$original = new NepaliDate();
$copy = new NepaliDate($original);From Timestamp
Creates a NepaliDate from a Unix timestamp in milliseconds.
new NepaliDate(int $timestampMs)Example:
$timestampMs = (int) round(microtime(true) * 1000);
$nepaliDate = new NepaliDate($timestampMs);Timestamp vs. Components
A bare int is always treated as a millisecond timestamp. To build a date from year/month/day, pass all three arguments — see From Components below.
From Date String
Creates a NepaliDate from a formatted BS date string.
new NepaliDate(string $dateString)Supported formats:
YYYY-MM-DD(e.g.,'2082-10-01')YYYY/MM/DD(e.g.,'2082/10/01')YYYY.MM.DD(e.g.,'2082.10.01')
Example:
$date = new NepaliDate('2082-10-15');
echo $date->getYear(), PHP_EOL; // 2082
echo $date->getMonth(), PHP_EOL; // 9 (0-indexed)
echo $date->getDate(), PHP_EOL; // 15From Components
Creates a NepaliDate with specific year, month, and day values.
new NepaliDate(int $year, int $month, int $day)Parameters:
$year- Nepali year (e.g., 2082)$month- Nepali month (0-11, where 0 = Baisakh)$day- Day of month (1-32)
Month is 0-indexed
The month parameter is 0-indexed: 0 = Baisakh, 1 = Jestha, ..., 11 = Chaitra — matching the NodeJS and Python ports.
Example:
// Magh 15, 2082 (month 9 = Magh since 0-indexed)
$date = new NepaliDate(2082, 9, 15);
echo $date->format('MMMM DD, YYYY'), PHP_EOL; // 'Magh 15, 2082'Getter Methods
getYear()
Returns the Nepali year.
public function getYear(): intReturns: Nepali year (e.g., 2082)
Example:
$date = new NepaliDate(2082, 9, 15);
echo $date->getYear(), PHP_EOL; // 2082getMonth()
Returns the Nepali month (0-indexed).
public function getMonth(): intReturns: Nepali month (0-11, where 0 = Baisakh, 11 = Chaitra)
Example:
$date = new NepaliDate(2082, 9, 15);
echo $date->getMonth(), PHP_EOL; // 9 (Magh)getDate()
Returns the day of the month.
public function getDate(): intReturns: Day of month (1-32)
Example:
$date = new NepaliDate(2082, 9, 15);
echo $date->getDate(), PHP_EOL; // 15getDay()
Returns the day of the week, computed in UTC.
public function getDay(): intReturns: Day of week (0-6, where 0 = Sunday, 6 = Saturday)
Example:
$date = new NepaliDate(2082, 9, 15);
echo $date->getDay(), PHP_EOL; // 3 (Wednesday)getHours()
Returns the hour component, computed in UTC.
public function getHours(): intReturns: Hour (0-23)
getMinutes()
Returns the minutes component, computed in UTC.
public function getMinutes(): intReturns: Minutes (0-59)
getSeconds()
Returns the seconds component, computed in UTC.
public function getSeconds(): intReturns: Seconds (0-59)
getMilliseconds()
Returns the milliseconds component.
public function getMilliseconds(): intReturns: Milliseconds (0-999)
getTime()
Returns the timestamp in milliseconds since the Unix epoch.
public function getTime(): intReturns: Timestamp in milliseconds
Example:
$date = new NepaliDate();
echo $date->getTime(), PHP_EOL; // 1736848800000getEnglishDate()
Returns the equivalent Gregorian (AD) date as a UTC DateTimeImmutable.
public function getEnglishDate(): DateTimeImmutableReturns: DateTimeImmutable in UTC
Example:
$nepaliDate = new NepaliDate(2082, 9, 1);
$englishDate = $nepaliDate->getEnglishDate();
echo $englishDate->format(DateTimeInterface::ATOM), PHP_EOL; // '2026-01-14T00:00:00+00:00'Setter Methods
setYear()
Sets the Nepali year.
public function setYear(int $year): voidParameters:
$year- Nepali year to set
Example:
$date = new NepaliDate(2082, 9, 15);
$date->setYear(2083);
echo $date->getYear(), PHP_EOL; // 2083setMonth()
Sets the Nepali month.
public function setMonth(int $month): voidParameters:
$month- Nepali month (0-11)
setDate()
Sets the day of month.
public function setDate(int $day): voidParameters:
$day- Day of month (1-32)
set()
Sets year, month, and day at once.
public function set(int $year, int $month, int $date): voidParameters:
$year- Nepali year$month- Nepali month (0-11)$date- Day of month (1-32)
Example:
$date = new NepaliDate();
$date->set(2082, 9, 15);
echo $date->toString(), PHP_EOL; // '2082/10/15'Formatting Methods
format()
Formats the date according to a format string.
public function format(string $formatStr): stringParameters:
$formatStr- Format pattern string
Returns: Formatted date string
English Format Tokens (Uppercase)
| Token | Description | Example |
|---|---|---|
YYYY | Full year | 2082 |
YY | 2-digit year | 82 |
MM | Month with leading zero | 01-12 |
M | Month without leading zero | 1-12 |
MMM | Short month name | Bai, Jes, Mag |
MMMM | Full month name | Baisakh, Magh |
DD | Day with leading zero | 01-32 |
D | Day without leading zero | 1-32 |
DDD | Short day name | Sun, Mon |
DDDD | Full day name | Sunday, Monday |
Nepali Format Tokens (Lowercase)
| Token | Description | Example |
|---|---|---|
yyyy | Full year in Nepali | २०८२ |
yy | 2-digit year in Nepali | ८२ |
mm | Month with leading zero | ०१-१२ |
m | Month without leading zero | १-१२ |
mmm | Short month name | बै, जे, मा |
mmmm | Full month name | बैशाख, माघ |
dd | Day with leading zero | ०१-३२ |
d | Day without leading zero | १-३२ |
ddd | Short day name | आइत, सोम |
dddd | Full day name | आइतबार, सोमबार |
Wrap literal text in double quotes ("...") to keep it from being interpreted as a token, e.g. "M"MM outputs M01.
Examples:
$date = new NepaliDate(2082, 9, 15);
// English formats
echo $date->format('YYYY-MM-DD'), PHP_EOL; // '2082-10-15'
echo $date->format('MMMM DD, YYYY'), PHP_EOL; // 'Magh 15, 2082'
echo $date->format('MMM D, YYYY'), PHP_EOL; // 'Mag 15, 2082'
echo $date->format('DDDD, MMMM DD'), PHP_EOL; // 'Wednesday, Magh 15'
// Nepali formats
echo $date->format('yyyy-mm-dd'), PHP_EOL; // '२०८२-१०-१५'
echo $date->format('mmmm dd, yyyy'), PHP_EOL; // 'माघ १५, २०८२'
echo $date->format('dddd, mmmm dd'), PHP_EOL; // 'बुधबार, माघ १५'
// Mixed format with literal text
echo $date->format('YYYY"/"MM"/"DD'), PHP_EOL; // '2082/10/15'toString()
Returns the date as a string in YYYY/M/D format with a 1-indexed month, unpadded.
public function toString(): stringReturns: Date string (e.g., '2082/10/15')
Example:
$date = new NepaliDate(2082, 9, 15);
echo $date->toString(), PHP_EOL; // '2082/10/15'
echo $date, PHP_EOL; // same, via __toString()TIP
NepaliDate implements __toString(), so instances can be interpolated directly into strings or passed anywhere PHP expects a string-castable value.
parse()
Parses a date string and updates the current instance.
public function parse(string $dateString): voidParameters:
$dateString- Date string in formatYYYY-MM-DD,YYYY/MM/DD, orYYYY.MM.DD
Example:
$date = new NepaliDate();
$date->parse('2082-10-15');
echo $date->getYear(), PHP_EOL; // 2082
echo $date->getMonth(), PHP_EOL; // 9
echo $date->getDate(), PHP_EOL; // 15Date Manipulation Methods
addDays()
Adds the specified number of days and returns a new instance.
public function addDays(int $days): selfParameters:
$days- Number of days to add (can be negative)
Returns: New NepaliDate instance
Example:
$date = new NepaliDate(2082, 9, 15);
$tomorrow = $date->addDays(1);
$lastWeek = $date->addDays(-7);
echo $tomorrow->format('YYYY-MM-DD'), PHP_EOL; // '2082-10-16'addMonths()
Adds the specified number of months and returns a new instance.
public function addMonths(int $months): selfParameters:
$months- Number of months to add (can be negative)
Returns: New NepaliDate instance
Example:
$date = new NepaliDate(2082, 9, 15);
$nextMonth = $date->addMonths(1);
echo $nextMonth->format('YYYY-MM-DD'), PHP_EOL; // '2082-11-15'addYears()
Adds the specified number of years and returns a new instance.
public function addYears(int $years): selfParameters:
$years- Number of years to add (can be negative)
Returns: New NepaliDate instance
Example:
$date = new NepaliDate(2082, 9, 15);
$nextYear = $date->addYears(1);
echo $nextYear->format('YYYY-MM-DD'), PHP_EOL; // '2083-10-15'Date Comparison Methods
diff()
Calculates the difference between two dates.
public function diff(NepaliDate $date, string $unit): intParameters:
$date-NepaliDateto compare with$unit- Unit of difference:'year','month', or'day'
Returns: Difference in the specified unit
Example:
$date1 = new NepaliDate(2082, 5, 10);
$date2 = new NepaliDate(2082, 5, 20);
echo $date1->diff($date2, 'day'), PHP_EOL; // -10
echo $date2->diff($date1, 'day'), PHP_EOL; // 10
echo $date1->diff($date2, 'month'), PHP_EOL; // 0isAfter()
Checks if this date comes after the specified date.
public function isAfter(NepaliDate $date): boolParameters:
$date- Date to compare with
Returns: true if this date is after the specified date
Example:
$date1 = new NepaliDate(2082, 9, 20);
$date2 = new NepaliDate(2082, 9, 15);
var_dump($date1->isAfter($date2)); // trueisBefore()
Checks if this date comes before the specified date.
public function isBefore(NepaliDate $date): boolParameters:
$date- Date to compare with
Returns: true if this date is before the specified date
Example:
$date1 = new NepaliDate(2082, 9, 10);
$date2 = new NepaliDate(2082, 9, 15);
var_dump($date1->isBefore($date2)); // trueisEqual()
Checks if this date is exactly equal to the specified date (year, month, day).
public function isEqual(NepaliDate $date): boolParameters:
$date- Date to compare with
Returns: true if dates are equal
Example:
$date1 = new NepaliDate(2082, 9, 15);
$date2 = new NepaliDate(2082, 9, 15);
var_dump($date1->isEqual($date2)); // trueisSame()
Checks if this date is the same as the specified date for the given unit.
public function isSame(NepaliDate $date, string $unit): boolParameters:
$date- Date to compare with$unit- Unit to compare:'year','month', or'day'
Returns: true if dates are the same for the specified unit
Example:
$date1 = new NepaliDate(2082, 9, 10);
$date2 = new NepaliDate(2082, 9, 20);
var_dump($date1->isSame($date2, 'year')); // true
var_dump($date1->isSame($date2, 'month')); // true
var_dump($date1->isSame($date2, 'day')); // falseDate Range Methods
startOfDay()
Returns a new NepaliDate set to the start of the current day (00:00:00 UTC).
public function startOfDay(): selfReturns: New NepaliDate at start of day
endOfDay()
Returns a new NepaliDate set to the end of the current day (23:59:59.999 UTC).
public function endOfDay(): selfReturns: New NepaliDate at end of day
startOfWeek()
Returns a new NepaliDate representing the start of the week.
public function startOfWeek(int $startOfWeek = 0): selfParameters:
$startOfWeek- Day to consider as start of week (0-6, 0 = Sunday). Default:0
Returns: New NepaliDate at start of week
Example:
$date = new NepaliDate(2082, 9, 15); // Wednesday
$weekStart = $date->startOfWeek(); // Previous Sunday
$weekStartMon = $date->startOfWeek(1); // Previous MondayendOfWeek()
Returns a new NepaliDate representing the end of the week.
public function endOfWeek(int $startOfWeek = 0): selfParameters:
$startOfWeek- Day to consider as start of week (0-6, 0 = Sunday). Default:0
Returns: New NepaliDate at end of week
startOfMonth()
Returns a new NepaliDate representing the first day of the month.
public function startOfMonth(): selfReturns: New NepaliDate at first day of month
Example:
$date = new NepaliDate(2082, 9, 15);
$monthStart = $date->startOfMonth();
echo $monthStart->format('YYYY-MM-DD'), PHP_EOL; // '2082-10-01'endOfMonth()
Returns a new NepaliDate representing the last day of the month.
public function endOfMonth(): selfReturns: New NepaliDate at last day of month
Example:
$date = new NepaliDate(2082, 9, 15);
$monthEnd = $date->endOfMonth();
echo $monthEnd->format('YYYY-MM-DD'), PHP_EOL; // '2082-10-29'startOfYear()
Returns a new NepaliDate representing the first day of the year (1st Baisakh).
public function startOfYear(): selfReturns: New NepaliDate at first day of year
Example:
$date = new NepaliDate(2082, 9, 15);
$yearStart = $date->startOfYear();
echo $yearStart->format('YYYY-MM-DD'), PHP_EOL; // '2082-01-01'endOfYear()
Returns a new NepaliDate representing the last day of the year (last day of Chaitra).
public function endOfYear(): selfReturns: New NepaliDate at last day of year
Date Information Methods
daysInMonth()
Returns the number of days in a given (or the current) month.
public function daysInMonth(?int $year = null, ?int $month = null): intParameters:
$year- Nepali year (optional, defaults to this instance's year)$month- Nepali month, 0-11 (optional, defaults to this instance's month)
Returns: Number of days (29-32)
Example:
$date = new NepaliDate(2082, 9, 15); // Magh 2082
echo $date->daysInMonth(), PHP_EOL; // 30
// Check any month statically without a matching instance
echo $date->daysInMonth(2081, 5), PHP_EOL;isLeapYear()
Checks if the current year is a leap year in the Nepali calendar.
public function isLeapYear(): boolReturns: true if leap year
getWeeksInMonth()
Calculates the number of weeks in the current month.
public function getWeeksInMonth(): intReturns: Number of weeks
Quarter Methods
getCurrentQuarter()
Returns the quarter number (1-4) for the current date.
public function getCurrentQuarter(): intReturns: Quarter number (1-4)
Example:
$date = new NepaliDate(2082, 9, 15); // Magh = Q4
echo $date->getCurrentQuarter(), PHP_EOL; // 4getCurrentFiscalYearQuarter()
Returns the current fiscal year quarter number (1-4).
public function getCurrentFiscalYearQuarter(): intReturns: Fiscal year quarter number (1-4)
Fiscal Year
Nepal's fiscal year starts from Shrawan 1st (month index 3). So:
- Q1: Shrawan - Aswin (months 3-5)
- Q2: Kartik - Poush (months 6-8)
- Q3: Magh - Chaitra (months 9-11)
- Q4: Baisakh - Asar (months 0-2)
getCurrentFiscalYearQuarterDates()
Returns the start and end dates of the current fiscal year quarter.
public function getCurrentFiscalYearQuarterDates(): array{start: self, end: self}Returns: Array with 'start' and 'end' NepaliDate values
Arrays, not objects
Wherever the NodeJS/Python ports return an object with start/end properties, the PHP port returns an associative array with 'start'/'end' keys — access them as $result['start'] / $result['end'].
Utility Methods
clone()
Creates a copy of the current NepaliDate instance.
public function clone(): selfReturns: New NepaliDate with the same date and time
Example:
$date = new NepaliDate(2082, 9, 15);
$copy = $date->clone();
$copy->setDate(20);
echo $date->getDate(), PHP_EOL; // 15 (unchanged)
echo $copy->getDate(), PHP_EOL; // 20isValidInstance()
Checks if the current NepaliDate instance contains a valid date.
public function isValidInstance(): boolReturns: true if valid
Named differently from NodeJS/Python
NodeJS and Python expose an instance method named isValid(). PHP cannot have a static and an instance method share the same name, and NepaliDate::isValid() is already used for the static validator — so the instance check is named isValidInstance() here.