将 SQLite 转换为 MySQL 日期时间

问题描述 投票:0回答:3

将 SQLite DB 实际值转换为 mysql 中的日期时间

attach image for table structure

该字段的值为18696.0

我想将 SQLite 的这个实值字段转换为 Mysqli 日期时间。

php mysql sqlite date datetime
3个回答
1
投票

编辑:感谢您使用正确的数字及其代表的日期更新您的问题。

您可以使用将日期添加到 Unix Epoch 日期的函数来实现您所需要的:

function realDateToYmd($real, $outputFormat='Y-m-d')
{
    $date = new DateTime('1970-01-01');
    $date->modify('+' . intval($real) . ' days');
    
    return $date->format($outputFormat);
}

echo realDateToYmd('18696.0');
// returns 2021-03-10

存储在

REAL
数据类型中的 SQLite 日期将日期存储为 儒略日

来自 https://www.sqlite.org/datatype3.html

REAL 作为儒略日数字,自公元前 4714 年 11 月 24 日格林威治中午以来的天数根据公历。

PHP 有一个

jdtogregorian
函数,其中一条注释有一个方便的函数可以转换为 ISO8601 日期:

function JDtoISO8601($JD) {
    if ($JD <= 1721425) $JD += 365;
    list($month, $day, $year) = explode('/', jdtogregorian($JD));
    return sprintf('%+05d-%02d-%02d', $year, $month, $day);
}

echo JDtoISO8601('17889.0');
// Results in -4664-11-16

结果看起来不太正确,在 SQLite 中肯定是 17889.0 吗?


0
投票

如果这个浮点数 18696.0 代表自 1970-01-01 以来的天数,那么日期也可以这样计算:

$days = 18696.0;

$dt = date_create('@'.((int)($days * 86400)));
$mysqlDate = $dt->format('Y-m-d');  //"2021-03-10"

背景资料

或者简单地使用 gmdate:

$mySqlDate = gmdate('Y-m-d',$days*86400);

将天数简单地转换为秒以获得 gmdate 的有效时间戳。


-1
投票

试试这个:

<?php
    echo date('Y-m-d H:i:s', 17889);
?>

输出:

1970-01-01 04:58:09

© www.soinside.com 2019 - 2024. All rights reserved.