将SQLite转换为MySQL日期时间

bjp0bcyl  于 2022-11-30  发布在  SQLite
关注(0)|答案(2)|浏览(176)

我有一个SQLite DB表包含日期时间字段
数据类型为“timestamp”的真实的值为18696.0
attach image for table structure
因此,我希望将此18696.0值转换为MySQL Y-m-d格式,结果应为2021-03-10
我在网上没有找到任何解决方案。任何帮助将不胜感激。
SQLite时间戳转换为MySQL时间戳。

drkbr07n

drkbr07n1#

试试这个:

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

输出:1970-01-01 04:58:09

mdfafbf1

mdfafbf12#

编辑:感谢您更新您的问题与正确的数字和它应该代表什么日期。
您可以使用一个函数将日期添加到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日期将日期存储为Julian Day
https://www.sqlite.org/datatype3.html开始
真实的作为儒略日数,是指根据格里高利历,自公元前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吗?

相关问题