Created
September 24, 2013 10:21
-
-
Save seanbamforth/6682882 to your computer and use it in GitHub Desktop.
PHP OADate formatting - To and From...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
if ( ! function_exists('toOADate')) | |
{ | |
function toOADate($timestamp) { | |
$from_date = new DateTime("1899-12-30"); | |
$to_date = new DateTime($timestamp); | |
$interval = $from_date->diff($to_date); | |
return $interval -> days; | |
} | |
function OADate_to_str($duration, $format="Y-m-d") { | |
$adate = new DateTime("1899-12-30"); | |
$adate -> modify( "+{$duration} day"); | |
return $adate -> format($format); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seanbamforth and @DaveRandom, thanks for the gist. When using it to populate a Google Sheet, minor deviations seemed to occur:
2020-06-25 18:00:00
in PHP becomes2020-06-25 17:59:32
in Google Sheets. These missing 28 seconds "might" occur because DateInterval does not support leap seconds?But also
2020-10-29 18:00:00
becomes2020-10-29 17:19:32
, which I cannot explain...Using https://github.com/PHPOffice/PhpSpreadsheet/blob/0ed5b800be2136bcb8fa9c1bdf59abc957a98845/src/PhpSpreadsheet/Shared/Date.php#L240 prevented these deviations