Skip to content

Commit

Permalink
Update Doc Blocks to Discourage Use of Unix Timestamps
Browse files Browse the repository at this point in the history
This was suggested by issue PHPOffice#2347. Unix timestamps have clear disadvantages compared with the alternate methods of supplying date and time to PhpSpreadsheet - DateTime objects, Excel date time fields, and strings. In particular, Unix timestamp is not Y2038-safe on a 32-bit system, and it reflects the time in UTC, which may come as a surprise to the end-user (as it did in the cited issue). The alternate methods do not come with such baggage. This change updates some doc blocks to note that Unix timestamps are discoburage (N.B. - not deprecated). No executable code is changed.
  • Loading branch information
oleibman committed Oct 24, 2021
1 parent f50c287 commit 4ef3912
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class Functions
*/
const M_2DIVPI = 0.63661977236758134307553505349006;

/** constants */
const COMPATIBILITY_EXCEL = 'Excel';
const COMPATIBILITY_GNUMERIC = 'Gnumeric';
const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc';

/** Use of RETURNDATE_PHP_NUMERIC is discouraged. */
const RETURNDATE_PHP_NUMERIC = 'P';
/** Use of RETURNDATE_UNIX_TIMESTAMP is discouraged. */
const RETURNDATE_UNIX_TIMESTAMP = 'P';
const RETURNDATE_PHP_OBJECT = 'O';
const RETURNDATE_PHP_DATETIME_OBJECT = 'O';
Expand Down
8 changes: 5 additions & 3 deletions src/PhpSpreadsheet/Shared/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static function validateTimeZone($timeZone)
* @param float|int $excelTimestamp MS Excel serialized date/time value
* @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
* if you don't want to treat it as a UTC value
* Use the default (UST) unless you absolutely need a conversion
* Use the default (UTC) unless you absolutely need a conversion
*
* @return DateTime PHP date/time object
*/
Expand Down Expand Up @@ -206,11 +206,12 @@ public static function excelToDateTimeObject($excelTimestamp, $timeZone = null)

/**
* Convert a MS serialized datetime value from Excel to a unix timestamp.
* The use of Unix timestamps, and therefore this function, is discouraged.
*
* @param float|int $excelTimestamp MS Excel serialized date/time value
* @param null|DateTimeZone|string $timeZone The timezone to assume for the Excel timestamp,
* if you don't want to treat it as a UTC value
* Use the default (UST) unless you absolutely need a conversion
* Use the default (UTC) unless you absolutely need a conversion
*
* @return int Unix timetamp for this date/time
*/
Expand All @@ -223,7 +224,7 @@ public static function excelToTimestamp($excelTimestamp, $timeZone = null)
/**
* Convert a date from PHP to an MS Excel serialized date/time value.
*
* @param mixed $dateValue Unix Timestamp or PHP DateTime object or a string
* @param mixed $dateValue PHP DateTime object or a string - Unix timestamp is also permitted, but discouraged
*
* @return bool|float Excel date/time value
* or boolean FALSE on failure
Expand Down Expand Up @@ -262,6 +263,7 @@ public static function dateTimeToExcel(DateTimeInterface $dateValue)

/**
* Convert a Unix timestamp to an MS Excel serialized date/time value.
* The use of Unix timestamps, and therefore this function, is discouraged.
*
* @param int $dateValue Unix Timestamp
*
Expand Down

0 comments on commit 4ef3912

Please sign in to comment.