fyre/datetime

A DateTime library.

v3.0.6 2024-06-29 02:12 UTC

README

FyreDateTime is a free, open-source immutable date manipulation library for PHP.

It is a modern library, and features support for ICU formats, time zones and locales.

Table Of Contents

Installation

Using Composer

composer require fyre/datetime

In PHP:

use Fyre\DateTime\DateTime;

Date Creation

  • $dateString is a string representing the date, and will default to the current timestamp.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$dateTime = new DateTime($dateString, $timeZone, $locale);

From Array

  • $dateArray is an array containing the year, month, date, hours, minutes, seconds and milliseconds.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$dateTime = DateTime::fromArray($dateArray, $timeZone, $locale);

The month and date in the $dateArray will default to 1 if not set. The hours, minutes, seconds and milliseconds will default to 0.

From DateTime

  • $dateTime is an instance of a class implementing DateTimeInterface.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$newDateTime = DateTime::fromDateTime($dateTime, $timeZone, $locale);

From Format

  • $formatString is a string containing the format you wish to use for parsing.
  • $dateString is a string representing the date you are parsing.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.

The $formatString supports tokens described in the ICU specification.

$dateTime = DateTime::fromFormat($formatString, $dateString, $timeZone, $locale);

From ISO String

  • $dateString is a string representing the date you are parsing.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$dateTime = DateTime::fromISOString($dateString, $timeZone, $locale);

From Timestamp

  • $timestamp is the number of seconds since the UNIX epoch.
  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$dateTime = DateTime::fromTimestamp($timestamp, $timeZone, $locale);

Now

  • $timeZone is a string representing the time zone of the date, and will default to the system time zone.
  • $locale is a string representing the locale of the date, and will default to the system locale.
$dateTime = DateTime::now($timeZone, $locale);

Date Formatting

Format

Once you have created a DateTime object, you can get a string representation using a specific format with the format method.

  • $formatString is a string containing the format you wish to output using.

The $formatString supports tokens described in the ICU specification.

$dateString = $dateTime->format($formatString);

To String

Format the current date using "eee MMM dd yyyy HH:mm:ss xx (VV)".

$string = $dateTime->toString();

To Date String

Format the current date using "eee MMM dd yyyy".

$dateString = $dateTime->toDateString();

To ISO String

Format the current date using "yyyy-MM-dd'T'HH:mm:ss.SSSxxx" (in English and UTC time zone).

$isoString = $dateTime->toISOString();

To Time String

Format the current date using "HH:mm:ss xx (VV)".

$timeString = $dateTime->toTimeString();

To UTC String

Format the current date using "eee MMM dd yyyy HH:mm:ss xx (VV)" (in UTC time zone).

$utcString = $dateTime->toUTCString();

Date Attributes

Get Date

Get the date in current time zone.

$date = $dateTime->getDate();

Get Day

Get the day of the week in current time zone.

The $day returned will be between 0 (Sunday) and 6 (Saturday).

$day = $dateTime->getDay();

Get Day Of Year

Get the day of the year in current time zone.

The $dayOfYear returned will be between 0 and 365.

$dayOfYear = $dateTime->getDayOfYear();

Get Month

Get the month in current time zone.

The $month returned will be between 1 (January) and 12 (December).

$month = $dateTime->getMonth();

Get Quarter

Get the quarter of the year in current time zone.

The $quarter returned will be between 1 and 4.

$quarter = $dateTime->getQuarter();

Get Year

Get the year in current time zone.

$year = $dateTime->getYear();

Set Date

Set the date in current time zone.

  • $date is a number representing the date.
$newDate = $dateTime->setDate($date);

Set Day

Set the day of the week in current time zone.

  • $day is a number representing the day of the week (between 0 and 6).
$newDateTime = $dateTime->setDay($day);

Set Day Of Year

Set the day of the year in current time zone.

  • $dayOfYear is a number representing the day of the year (between 0 and 365).
$newDateTime = $dateTime->setDayOfYear($dayOfYear);

Set Month

Set the month in current time zone.

  • $month is a number representing the month (between 1 and 12).
  • $date is a number representing the date, and will default to the current value.

If the $date argument is omitted, and the new month contains less days than the current date, the date will be set to the last day of the new month.

To disable date clamping, use the method DateTime::setDateClamping() using false as the argument.

$newDateTime = $dateTime->setMonth($month, $date);

Set Quarter

Set the quarter of the year in current time zone.

  • $quarter is a number representing the quarter between 1 and 4.
$newDateTime = $dateTime->setQuarter($quarter);

Set Year

Set the year in current time zone.

  • $year is a number representing the year.
  • $month is a number representing the month (between 1 and 12), and will default to the current value.
  • $date is a number representing the date, and will default to the current value.

If the $date argument is omitted, and the new month contains less days than the current date, the date will be set to the last day of the new month.

To disable date clamping, use the method DateTime::setDateClamping() using false as the argument.

$newDateTime = $dateTime->setYear($year, $month, $date);

Week Attributes

Get Week

Get the week of the year in current time zone.

The $week returned will be between 1 and 53 (week starting on Monday).

$week = $dateTime->getWeek();

Get Week Day

Get the local day of the week in current time zone.

The $weekDay returned will be between 1 and 7.

$weekDay = $dateTime->getWeekDay();

Get Week Day In Month

Get the day of the week in the month, in current time zone.

The $weekDayInMonth returned will be between 1 and 5.

$weekDayInMonth = $dateTime->getWeekDayInMonth();

Get Week Of Month

Get the week of the month in current time zone.

The $weekOfMonth returned will be between 1 and 5.

$weekOfMonth = $dateTime->getWeekOfMonth();

Get Week Year

Get the week year in current time zone.

This method is identical to getYear() except in cases where the week belongs to the previous or next year, then that value will be used instead.

$weekYear = $dateTime->getWeekYear();

Set Week

Set the week in current time zone.

  • $week is a number representing the week.
  • $weekDay is a number representing the day (between 1 and 7), and will default to the current value.
$newDateTime = $dateTime->setWeek($week, $weekDay);

Set Week Day

Set the local day of the week in current time zone.

  • $weekDay is a number representing the week day (between 1 and 7).
$newDateTime = $dateTime->setWeekDay($weekDay);

Set Week Day In Month

Set the day of the week in the month, in current time zone.

  • $weekDayInMonth is a number representing the day of the week in month (between 1 and 5).
$newDateTime = $dateTime->setWeekDayInMonth($weekDayInMonth);

Set Week Of Month

Set the week of the month in current time zone.

  • $weekOfMonth is a number representing the week of the month (between 1 and 5).
$newDateTime = $dateTime->setWeekOfMonth($weekOfMonth);

Set Week Year

Set the week year in current time zone.

  • $weekYear is a number representing the year.
  • $week is a number representing the week, and will default to the current value.
  • $weekDay is a number representing the day (between 1 and 7), and will default to the current value.
$newDateTime = $dateTime->setWeekYear($weekYear, $week, $weekDay);

Time Attributes

Get Hours

Get the hours of the day in current time zone.

The $hours returned will be between 0 and 23.

$hours = $dateTime->getHours();

Get Milliseconds

Get the milliseconds of the second in current time zone.

The $milliseconds returned will be between 0 and 999.

$milliseconds = $dateTime->getMilliseconds();

Get Minutes

Get the minutes of the hour in current time zone.

The $minutes returned will be between 0 and 59.

$minutes = $dateTime->getMinutes();

Get Seconds

Get the seconds of the minute in current time zone.

The $seconds returned will be between 0 and 59.

$seconds = $dateTime->getSeconds();

Set Hours

Set the hours of the day in current time zone.

  • $hours is a number representing the hours of the day (between 0 and 23).
  • $minutes is a number representing the minutes of the hour (between 0 and 59), and will default to the current value.
  • $seconds is a number representing the seconds of the minute (between 0 and 59), and will default to the current value.
  • $milliseconds is a number representing the milliseconds of the second (between 0 and 999), and will default to the current value.
$newDateTime = $dateTime->setHours($hours, $minutes, $seconds, $milliseconds);

Set Milliseconds

Set the milliseconds of the second in current time zone.

  • $milliseconds is a number representing the milliseconds of the second (between 0 and 999).
$newDateTime = $dateTime->setMilliseconds($milliseconds);

Set Minutes

Set the minutes of the hour in current time zone.

  • $minutes is a number representing the minutes of the hour (between 0 and 59).
  • $seconds is a number representing the seconds of the minute (between 0 and 59), and will default to the current value.
  • $milliseconds is a number representing the milliseconds of the second (between 0 and 999), and will default to the current value.
$newDateTime = $dateTime->setMinutes($minutes, $seconds, $milliseconds);

Set Seconds

Set the seconds of the minute in current time zone.

  • $seconds is a number representing the seconds of the minute (between 0 and 59).
  • $milliseconds is a number representing the milliseconds of the second (between 0 and 999), and will default to the current value.
$newDateTime = $dateTime->setSeconds($seconds, $milliseconds);

Timestamps

Get Milliseconds

Get the number of milliseconds since the UNIX epoch.

$time = $dateTime->getTime();

Get Seconds

Get the number of seconds since the UNIX epoch.

$timestamp = $dateTime->getTimestamp();

Set Milliseconds

Set the number of milliseconds since the UNIX epoch.

$newDateTime = $dateTime->setTime($time);

Set Seconds

Set the number of seconds since the UNIX epoch.

$newDateTime = $dateTime->setTimestamp($timestamp);

Time Zones

Get Time Zone

Get the name of the current time zone.

$timeZone = $dateTime->getTimeZone();

Get Time Zone Offset

Get the UTC offset (in minutes) of the current time zone.

$offset = $dateTime->getTimeZoneOffset();

Set Time Zone

Set the current time zone.

  • $timeZone is the name of the new time zone, which can be either "UTC", a supported value from the IANA timeZone database or an offset string.
$newDateTime = $dateTime->setTimeZone($timeZone);

Set Time Zone Offset

Set the UTC offset (in minutes).

  • $offset is the UTC offset (in minutes).
$newDateTime = $dateTime->setTimeZoneOffset($offset);

Locales

Get Locale

Get the name of the current locale.

$locale = $dateTime->getLocale();

Set Locale

Set the current locale.

  • $locale is the name of the new locale.
$newDateTime = $dateTime->setLocale($locale);

Manipulation

Add Day

Add a day to the current date.

$newDateTime = $dateTime->addDay();

Add Days

Add days to the current date.

  • $amount is a number representing the amount of days to add.
$newDateTime = $dateTime->addDay($amount);

Add Hour

Add a hour to the current date.

$newDateTime = $dateTime->addHour();

Add Hours

Add hours to the current date.

  • $amount is a number representing the amount of hours to add.
$newDateTime = $dateTime->addHours($amount);

Add Minute

Add a minute to the current date.

$newDateTime = $dateTime->addMinute();

Add Minutes

Add minutes to the current date.

  • $amount is a number representing the amount of minutes to add.
$newDateTime = $dateTime->addMinutes($amount);

Add Month

Add a month to the current date.

$newDateTime = $dateTime->addMonth();

Add Months

Add months to the current date.

  • $amount is a number representing the amount of months to add.
$newDateTime = $dateTime->addMonths($amount);

Add Second

Add a second to the current date.

$newDateTime = $dateTime->addSecond();

Add Seconds

Add seconds to the current date.

  • $amount is a number representing the amount of seconds to add.
$newDateTime = $dateTime->addSeconds($amount);

Add Week

Add a week to the current date.

$newDateTime = $dateTime->addWeek();

Add Weeks

Add weeks to the current date.

  • $amount is a number representing the amount of weeks to add.
$newDateTime = $dateTime->addWeeks($amount);

Add Year

Add a year to the current date.

$newDateTime = $dateTime->addYear();

Add Years

Add years to the current date.

  • $amount is a number representing the amount of years to add.
$newDateTime = $dateTime->addYears($amount);

End Of Day

Set the date to the end of the day in current time zone.

$newDateTime = $dateTime->endOfDay();

End Of Hour

Set the date to the end of the hour in current time zone.

$newDateTime = $dateTime->endOfHour();

End Of Minute

Set the date to the end of the minute in current time zone.

$newDateTime = $dateTime->endOfMinute();

End Of Month

Set the date to the end of the month in current time zone.

$newDateTime = $dateTime->endOfMonth();

End Of Quarter

Set the date to the end of the quarter in current time zone.

$newDateTime = $dateTime->endOfQuarter();

End Of Second

Set the date to the end of the second in current time zone.

$newDateTime = $dateTime->endOfSecond();

End Of Week

Set the date to the end of the week in current time zone.

$newDateTime = $dateTime->endOfWeek();

End Of Year

Set the date to the end of the year in current time zone.

$newDateTime = $dateTime->endOfYear();

Start Of Day

Set the date to the start of the day in current time zone.

$newDateTime = $dateTime->startOfDay();

Start Of Hour

Set the date to the start of the hour in current time zone.

$newDateTime = $dateTime->startOfHour();

Start Of Minute

Set the date to the start of the minute in current time zone.

$newDateTime = $dateTime->startOfMinute();

Start Of Month

Set the date to the start of the month in current time zone.

$newDateTime = $dateTime->startOfMonth();

Start Of Quarter

Set the date to the start of the quarter in current time zone.

$newDateTime = $dateTime->startOfQuarter();

Start Of Second

Set the date to the start of the second in current time zone.

$newDateTime = $dateTime->startOfSecond();

Start Of Week

Set the date to the start of the week in current time zone.

$newDateTime = $dateTime->startOfWeek();

Start Of Year

Set the date to the start of the year in current time zone.

$newDateTime = $dateTime->startOfYear();

Subtract Day

Subtract a day to the current date.

$newDateTime = $dateTime->subtractDay();

Subtract Days

Subtract days to the current date.

  • $amount is a number representing the amount of days to subtract.
$newDateTime = $dateTime->subtractDay($amount);

Subtract Hour

Subtract a hour to the current date.

$newDateTime = $dateTime->subtractHour();

Subtract Hours

Subtract hours to the current date.

  • $amount is a number representing the amount of hours to subtract.
$newDateTime = $dateTime->subtractHours($amount);

Subtract Minute

Subtract a minute to the current date.

$newDateTime = $dateTime->subtractMinute();

Subtract Minutes

Subtract minutes to the current date.

  • $amount is a number representing the amount of minutes to subtract.
$newDateTime = $dateTime->subtractMinutes($amount);

Subtract Month

Subtract a month to the current date.

$newDateTime = $dateTime->subtractMonth();

Subtract Months

Subtract months to the current date.

  • $amount is a number representing the amount of months to subtract.
$newDateTime = $dateTime->subtractMonths($amount);

Subtract Second

Subtract a second to the current date.

$newDateTime = $dateTime->subtractSecond();

Subtract Seconds

Subtract seconds to the current date.

  • $amount is a number representing the amount of seconds to subtract.
$newDateTime = $dateTime->subtractSeconds($amount);

Subtract Week

Subtract a week to the current date.

$newDateTime = $dateTime->subtractWeek();

Subtract Weeks

Subtract weeks to the current date.

  • $amount is a number representing the amount of weeks to subtract.
$newDateTime = $dateTime->subtractWeeks($amount);

Subtract Year

Subtract a year to the current date.

$newDateTime = $dateTime->subtractYear();

Subtract Years

Subtract years to the current date.

  • $amount is a number representing the amount of years to subtract.
$newDateTime = $dateTime->subtractYears($amount);

Comparisons

Difference

Get the difference between two dates in milliseconds.

  • $other is the DateTime object to compare to.
$diff = $dateTime->diff($other);

Difference In Days

Get the difference between two dates in days.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInDays($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Hours

Get the difference between two dates in hours.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInHours($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Minutes

Get the difference between two dates in minutes.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInMinutes($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Months

Get the difference between two dates in months.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInMonths($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Seconds

Get the difference between two dates in seconds.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInSeconds($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Weeks

Get the difference between two dates in weeks.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInWeeks($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Difference In Years

Get the difference between two dates in years.

  • $other is the DateTime object to compare to.
  • $relative is a boolean indicating whether to return the relative difference, and will default to true.
$diff = $dateTime->diffInYears($other, $relative);

If $relative is true (default) the value returned will be the relative difference, ignoring higher precision properties.

Is After?

Return true if the DateTime is after another date.

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfter($other);

Is After Day?

Return true if the DateTime is after another date (comparing by day).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterDay($other);

Is After Hour?

Return true if the DateTime is after another date (comparing by hour).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterHour($other);

Is After Minute?

Return true if the DateTime is after another date (comparing by minute).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterMinute($other);

Is After Month?

Return true if the DateTime is after another date (comparing by month).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterMonth($other);

Is After Second?

Return true if the DateTime is after another date (comparing by second).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterSecond($other);

Is After Week?

Return true if the DateTime is after another date (comparing by week).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterWeek($other);

Is After Year?

Return true if the DateTime is after another date (comparing by year).

  • $other is the DateTime object to compare to.
$isAfter = $dateTime->isAfterYear($other);

Is Before?

Return true if the DateTime is before another date.

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBefore($other);

Is Before Day?

Return true if the DateTime is before another date (comparing by day).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeDay($other);

Is Before Hour?

Return true if the DateTime is before another date (comparing by hour).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeHour($other);

Is Before Minute?

Return true if the DateTime is before another date (comparing by minute).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeMinute($other);

Is Before Month?

Return true if the DateTime is before another date (comparing by month).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeMonth($other);

Is Before Second?

Return true if the DateTime is before another date (comparing by second).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeSecond($other);

Is Before Week?

Return true if the DateTime is before another date (comparing by week).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeWeek($other);

Is Before Year?

Return true if the DateTime is before another date (comparing by year).

  • $other is the DateTime object to compare to.
$isBefore = $dateTime->isBeforeYear($other);

Is Between?

Return true if the DateTime is between two other dates.

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetween($start, $end);

Is Between Day?

Return true if the DateTime is between two other dates (comparing by day).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenDay($start, $end);

Is Between Hour?

Return true if the DateTime is between two other dates (comparing by hour).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenHour($start, $end);

Is Between Minute?

Return true if the DateTime is between two other dates (comparing by minute).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenMinute($start, $end);

Is Between Month?

Return true if the DateTime is between two other dates (comparing by month).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenMonth($start, $end);

Is Between Second?

Return true if the DateTime is between two other dates (comparing by second).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenSecond($start, $end);

Is Between Week?

Return true if the DateTime is between two other dates (comparing by week).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenWeek($start, $end);

Is Between Year?

Return true if the DateTime is between two other dates (comparing by year).

  • $start is the starting DateTime object to compare to.
  • $end is the ending DateTime object to compare to.
$isBetween = $dateTime->isBetweenYear($start, $end);

Is Same?

Return true if the DateTime is the same as another date.

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSame($other);

Is Same Day?

Return true if the DateTime is the same as another date (comparing by day).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameDay($other);

Is Same Hour?

Return true if the DateTime is the same as another date (comparing by hour).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameHour($other);

Is Same Minute?

Return true if the DateTime is the same as another date (comparing by minute).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameMinute($other);

Is Same Month?

Return true if the DateTime is the same as another date (comparing by month).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameMonth($other);

Is Same Second?

Return true if the DateTime is the same as another date (comparing by second).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameSecond($other);

Is Same Week?

Return true if the DateTime is the same as another date (comparing by week).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameWeek($other);

Is Same Year?

Return true if the DateTime is the same as another date (comparing by year).

  • $other is the DateTime object to compare to.
$isSame = $dateTime->isSameYear($other);

Is Same Or After?

Return true if the DateTime is the same as or after another date.

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfter($other);

Is Same Or After Day?

Return true if the DateTime is the same as or after another date (comparing by day).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterDay($other);

Is Same Or After Hour?

Return true if the DateTime is the same as or after another date (comparing by hour).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterHour($other);

Is Same Or After Minute?

Return true if the DateTime is the same as or after another date (comparing by minute).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterMinute($other);

Is Same Or After Month?

Return true if the DateTime is the same as or after another date (comparing by month).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterMonth($other);

Is Same Or After Second?

Return true if the DateTime is the same as or after another date (comparing by second).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterSecond($other);

Is Same Or After Week?

Return true if the DateTime is the same as or after another date (comparing by week).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterWeek($other);

Is Same Or After Year?

Return true if the DateTime is the same as or after another date (comparing by year).

  • $other is the DateTime object to compare to.
$isSameOrAfter = $dateTime->isSameOrAfterYear($other);

Is Same Or Before?

Return true if the DateTime is the same as or before another date.

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBefore($other);

Is Same Or Before Day?

Return true if the DateTime is the same as or before another date (comparing by day).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeDay($other);

Is Same Or Before Hour?

Return true if the DateTime is the same as or before another date (comparing by hour).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeHour($other);

Is Same Or Before Minute?

Return true if the DateTime is the same as or before another date (comparing by minute).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeMinute($other);

Is Same Or Before Month?

Return true if the DateTime is the same as or before another date (comparing by month).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeMonth($other);

Is Same Or Before Second?

Return true if the DateTime is the same as or before another date (comparing by second).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeSecond($other);

Is Same Or Before Week?

Return true if the DateTime is the same as or before another date (comparing by week).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeWeek($other);

Is Same Or Before Year?

Return true if the DateTime is the same as or before another date (comparing by year).

  • $other is the DateTime object to compare to.
$isSameOrBefore = $dateTime->isSameOrBeforeYear($other);

Utility Methods

Day Name

Get the name of the day of the week in current time zone and locale.

  • $type can be either "long", "short" or "narrow", and will default to "long" if it is not set.
$dayName = $dateTime->dayName($type);

Day Period

Get the day period in current time zone and locale.

  • $type can be either "long" or "short", and will default to "long" if it is not set.
$dayPeriod = $dateTime->dayPeriod($type);

Days In Month

Get the number of days in the current month.

$daysInMonth = $dateTime->daysInMonth();

Days In Year

Get the number of days in the current year.

$daysInYear = $dateTime->daysInYear();

Era

Get the era in current time zone and locale.

  • $type can be either "long", "short" or "narrow", and will default to "long" if it is not set.
$era = $dateTime->era($type);

Is DST?

Return true if the DateTime is in daylight savings.

$isDST = $dateTime->isDST();

Is Leap Year?

Return true if the year is a leap year.

$isLeapYear = $dateTime->isLeapYear();

Month Name

Get the name of the month in current time zone and locale.

  • $type can be either "long", "short" or "narrow", and will default to "long" if it is not set.
$monthName = $dateTime->monthName($type);

Time Zone Name

Get the name of the current time zone and locale.

  • $type can be either "long" or "short", and will default to "long" if it is not set.
$timeZoneName = $dateTime->timeZoneName($type);

Weeks In Year

Get the number of weeks in the current year.

$weeksInYear = $dateTime->weeksInYear();

Static Methods

Get Default Locale

Get the default locale.

$locale = DateTime::getDefaultLocale();

Get Default Time Zone

Get the default time zone.

$timeZone = DateTime::getDefaultTimeZone();

Set Date Clamping

Set whether dates will be clamped when changing months.

  • $clampDates is a boolean indicating whether to clamp dates.
DateTime::setDateClamping($clampDates);

Set Default Locale

Set the default locale.

  • $locale is the name of the locale.
DateTime::setDefaultLocale($locale);

Set Default Time Zone

Set the default time zone.

  • $timeZone is the name of the time zone, which can be either "UTC", a supported value from the IANA timeZone database or an offset string.
DateTime::setDefaultTimeZone($timeZone);