Documentation

Date
in package

Table of Contents

Constants

CALENDAR_MAC_1904  = 1904
CALENDAR_WINDOWS_1900  = 1900
constants

Properties

$monthNames  : array<string|int, string>
Names of the months of the year, indexed by shortname Planned usage for locale settings.
$numberSuffixes  : array<string|int, string>
$defaultTimeZone  : DateTimeZone|null
Default timezone to use for DateTime objects.
$excelCalendar  : int
Base calendar year to use for calculations Value is either CALENDAR_WINDOWS_1900 (1900) or CALENDAR_MAC_1904 (1904).

Methods

convertIsoDate()  : float|int
dateTimeFromTimestamp()  : DateTime
dateTimeToExcel()  : float
Convert a PHP DateTime object to an MS Excel serialized date/time value.
dayStringToNumber()  : int|string
Strips an ordinal from a numeric value.
excelToDateTimeObject()  : DateTime
Convert a MS serialized datetime value from Excel to a PHP Date/Time object.
excelToTimestamp()  : int
Convert a MS serialized datetime value from Excel to a unix timestamp.
formattedDateTimeFromTimestamp()  : string
formattedPHPToExcel()  : float
formattedPHPToExcel.
getDefaultOrLocalTimezone()  : DateTimeZone
Return the Default timezone, or local timezone if default is not set.
getDefaultTimezone()  : DateTimeZone
Return the Default timezone, or UTC if default not set.
getDefaultTimezoneOrNull()  : DateTimeZone|null
Return the Default timezone even if null.
getExcelCalendar()  : int
Return the Excel calendar (Windows 1900 or Mac 1904).
isDateTime()  : bool
Is a given cell a date/time?
isDateTimeFormat()  : bool
Is a given NumberFormat code a date/time format code?
isDateTimeFormatCode()  : bool
Is a given number format code a date/time?
monthStringToNumber()  : int|string
Converts a month name (either a long or a short name) to a month number.
PHPToExcel()  : false|float
Convert a date from PHP to an MS Excel serialized date/time value.
roundMicroseconds()  : void
Round the given DateTime object to seconds.
setDefaultTimezone()  : bool
Set the Default timezone to use for dates.
setExcelCalendar()  : bool
Set the Excel calendar (Windows 1900 or Mac 1904).
stringToExcel()  : false|float
Convert a date/time string to Excel time.
timestampToExcel()  : false|float
Convert a Unix timestamp to an MS Excel serialized date/time value.
safeModify()  : DateTime
Safely modifies a DateTime object using a specified modification string.

Constants

CALENDAR_MAC_1904

public mixed CALENDAR_MAC_1904 = 1904

CALENDAR_WINDOWS_1900

constants

public mixed CALENDAR_WINDOWS_1900 = 1900

Properties

$monthNames

Names of the months of the year, indexed by shortname Planned usage for locale settings.

public static array<string|int, string> $monthNames = ['Jan' => 'January', 'Feb' => 'February', 'Mar' => 'March', 'Apr' => 'April', 'May' => 'May', 'Jun' => 'June', 'Jul' => 'July', 'Aug' => 'August', 'Sep' => 'September', 'Oct' => 'October', 'Nov' => 'November', 'Dec' => 'December']

$numberSuffixes

public static array<string|int, string> $numberSuffixes = ['st', 'nd', 'rd', 'th']

$defaultTimeZone

Default timezone to use for DateTime objects.

protected static DateTimeZone|null $defaultTimeZone = null

$excelCalendar

Base calendar year to use for calculations Value is either CALENDAR_WINDOWS_1900 (1900) or CALENDAR_MAC_1904 (1904).

protected static int $excelCalendar = self::CALENDAR_WINDOWS_1900

Methods

convertIsoDate()

public static convertIsoDate(mixed $value[, int|null $calendar = null ]) : float|int
Parameters
$value : mixed

Converts a date/time in ISO-8601 standard format date string to an Excel serialized timestamp. See https://en.wikipedia.org/wiki/ISO_8601 for details of the ISO-8601 standard format.

$calendar : int|null = null
Return values
float|int

dateTimeFromTimestamp()

public static dateTimeFromTimestamp(string $date[, DateTimeZone|null $timeZone = null ]) : DateTime
Parameters
$date : string
$timeZone : DateTimeZone|null = null
Return values
DateTime

dateTimeToExcel()

Convert a PHP DateTime object to an MS Excel serialized date/time value.

public static dateTimeToExcel(DateTimeInterface $dateValue[, int|null $calendar = null ]) : float
Parameters
$dateValue : DateTimeInterface

PHP DateTime object

$calendar : int|null = null
Return values
float —

MS Excel serialized date/time value

dayStringToNumber()

Strips an ordinal from a numeric value.

public static dayStringToNumber(string $day) : int|string
Parameters
$day : string

Day number with an ordinal

Return values
int|string —

The integer value with any ordinal stripped, or the original string argument if it isn't a valid numeric

excelToDateTimeObject()

Convert a MS serialized datetime value from Excel to a PHP Date/Time object.

public static excelToDateTimeObject(float|int $excelTimestamp[, null|DateTimeZone|string $timeZone = null ][, int|null $calendar = null ]) : DateTime
Parameters
$excelTimestamp : float|int

MS Excel serialized date/time value

$timeZone : null|DateTimeZone|string = null

The timezone to assume for the Excel timestamp, if you don't want to treat it as a UTC value Use the default (UTC) unless you absolutely need a conversion

$calendar : int|null = null
Tags
throws
Exception
Return values
DateTime —

PHP date/time object

excelToTimestamp()

Convert a MS serialized datetime value from Excel to a unix timestamp.

public static excelToTimestamp(float|int $excelTimestamp[, null|DateTimeZone|string $timeZone = null ][, int|null $calendar = null ]) : int

The use of Unix timestamps, and therefore this function, is discouraged. They are not Y2038-safe on a 32-bit system, and have no timezone info.

Parameters
$excelTimestamp : float|int

MS Excel serialized date/time value

$timeZone : null|DateTimeZone|string = null

The timezone to assume for the Excel timestamp, if you don't want to treat it as a UTC value Use the default (UTC) unless you absolutely need a conversion

$calendar : int|null = null
Return values
int —

Unix timetamp for this date/time

formattedDateTimeFromTimestamp()

public static formattedDateTimeFromTimestamp(string $date, string $format[, DateTimeZone|null $timeZone = null ]) : string
Parameters
$date : string
$format : string
$timeZone : DateTimeZone|null = null
Return values
string

formattedPHPToExcel()

formattedPHPToExcel.

public static formattedPHPToExcel(int $year, int $month, int $day[, int $hours = 0 ][, int $minutes = 0 ][, float|int $seconds = 0 ][, int|null $calendar = null ]) : float
Parameters
$year : int
$month : int
$day : int
$hours : int = 0
$minutes : int = 0
$seconds : float|int = 0
$calendar : int|null = null
Return values
float —

Excel date/time value

getDefaultOrLocalTimezone()

Return the Default timezone, or local timezone if default is not set.

public static getDefaultOrLocalTimezone() : DateTimeZone
Return values
DateTimeZone

getDefaultTimezone()

Return the Default timezone, or UTC if default not set.

public static getDefaultTimezone() : DateTimeZone
Return values
DateTimeZone

getDefaultTimezoneOrNull()

Return the Default timezone even if null.

public static getDefaultTimezoneOrNull() : DateTimeZone|null
Return values
DateTimeZone|null

getExcelCalendar()

Return the Excel calendar (Windows 1900 or Mac 1904).

public static getExcelCalendar() : int
Return values
int —

Excel base date (1900 or 1904)

isDateTime()

Is a given cell a date/time?

public static isDateTime(Cell $cell[, mixed $value = null ][, bool $dateWithoutTimeOkay = true ]) : bool
Parameters
$cell : Cell
$value : mixed = null
$dateWithoutTimeOkay : bool = true
Return values
bool

isDateTimeFormat()

Is a given NumberFormat code a date/time format code?

public static isDateTimeFormat(NumberFormat $excelFormatCode[, bool $dateWithoutTimeOkay = true ]) : bool
Parameters
$excelFormatCode : NumberFormat
$dateWithoutTimeOkay : bool = true
Return values
bool

isDateTimeFormatCode()

Is a given number format code a date/time?

public static isDateTimeFormatCode(string $excelFormatCode[, bool $dateWithoutTimeOkay = true ]) : bool
Parameters
$excelFormatCode : string
$dateWithoutTimeOkay : bool = true
Return values
bool

monthStringToNumber()

Converts a month name (either a long or a short name) to a month number.

public static monthStringToNumber(string $monthName) : int|string
Parameters
$monthName : string

Month name or abbreviation

Return values
int|string —

Month number (1 - 12), or the original string argument if it isn't a valid month name

PHPToExcel()

Convert a date from PHP to an MS Excel serialized date/time value.

public static PHPToExcel(mixed $dateValue[, int|null $calendar = null ]) : false|float
Parameters
$dateValue : mixed

PHP DateTime object or a string - Unix timestamp is also permitted, but discouraged; not Y2038-safe on a 32-bit system, and no timezone info

$calendar : int|null = null
Return values
false|float —

Excel date/time value or boolean FALSE on failure

roundMicroseconds()

Round the given DateTime object to seconds.

public static roundMicroseconds(DateTime $dti) : void
Parameters
$dti : DateTime

setDefaultTimezone()

Set the Default timezone to use for dates.

public static setDefaultTimezone(null|DateTimeZone|string $timeZone) : bool
Parameters
$timeZone : null|DateTimeZone|string

The timezone to set for all Excel datetimestamp to PHP DateTime Object conversions

Return values
bool —

Success or failure

setExcelCalendar()

Set the Excel calendar (Windows 1900 or Mac 1904).

public static setExcelCalendar(int|null $baseYear) : bool
Parameters
$baseYear : int|null

Excel base date (1900 or 1904)

Return values
bool —

Success or failure

stringToExcel()

Convert a date/time string to Excel time.

public static stringToExcel(string $dateValue[, int|null $calendar = null ]) : false|float
Parameters
$dateValue : string

Examples: '2009-12-31', '2009-12-31 15:59', '2009-12-31 15:59:10'

$calendar : int|null = null
Return values
false|float —

Excel date/time serial value

timestampToExcel()

Convert a Unix timestamp to an MS Excel serialized date/time value.

public static timestampToExcel(float|int|string $unixTimestamp[, int|null $calendar = null ]) : false|float

The use of Unix timestamps, and therefore this function, is discouraged. They are not Y2038-safe on a 32-bit system, and have no timezone info.

Parameters
$unixTimestamp : float|int|string

Unix Timestamp

$calendar : int|null = null
Return values
false|float —

MS Excel serialized date/time value

safeModify()

Safely modifies a DateTime object using a specified modification string.

protected static safeModify(DateTime $dateTime, string $modifier) : DateTime

Prior to PHP 8.3, DateTime::modify() would return false on failure but would also emit an E_WARNING. Starting with PHP 8.3, DateTime::modify() throws a DateMalformedStringException on failure instead of returning false or emitting warnings.

This method provides consistent exception-based error handling across PHP versions:

  • For PHP 8.3+: Uses the native exception-throwing behavior of modify()
  • For PHP < 8.3: Converts warnings to exceptions using a custom error handler

This ensures that calling code can rely on exception handling for all date modification failures regardless of the PHP version in use.

Parameters
$dateTime : DateTime

The DateTime object to be modified.

$modifier : string

A modification string, such as '+1 day', '-2 hours', 'last Monday', etc.

Tags
throws
Exception

If an error occurs during the date modification process.

codeCoverageIgnore
Return values
DateTime —

The modified DateTime object.


        
On this page

Search results