Skip to content

Instantly share code, notes, and snippets.

@BlaM
Created March 23, 2017 13:07
Show Gist options
  • Save BlaM/06c6c248279c602d80c04cf8661d345e to your computer and use it in GitHub Desktop.
Save BlaM/06c6c248279c602d80c04cf8661d345e to your computer and use it in GitHub Desktop.
PHP implementation of mysql's TO_DAYS() and FROM_DAYS() functions
<?php
function TO_DAYS($date) {
if (is_numeric($date)) {
$res = 719528 + (int) ($date / 86400);
} else {
$TZ = date_default_timezone_get();
date_default_timezone_set('UTC');
$res = 719528 + (int) (strtotime($date) / 86400);
date_default_timezone_set($TZ);
}
return $res;
}
function FROM_DAYS($daystamp, $asTS = false) {
$ts = ($daystamp - 719528) * 86400;
return $asTS?$ts:gmdate('Y-m-d', $ts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment