Skip to content

Instantly share code, notes, and snippets.

@seanbamforth
Created September 24, 2013 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seanbamforth/6682882 to your computer and use it in GitHub Desktop.
Save seanbamforth/6682882 to your computer and use it in GitHub Desktop.
PHP OADate formatting - To and From...
<?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);
}
}
@DaveRandom
Copy link

@holtkamp
Copy link

holtkamp commented Nov 24, 2020

@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 becomes 2020-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 becomes 2020-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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment