将C ++ std :: chrono :: duration_cast <std :: chrono :: milliseconds>转换为PHP DateTime

I´m writing an application composed of a C++ process that acquires data and stores it in a Sqlite3 database, and a PHP Web application to analyze that data.

Some collected data generates DateTime formats, that are stored on a INTEGER type field on Sqlite3.

At the C++ side, the following piece of code is used to generate the timestamps (in milliseconds precision, as I´m dealing with a real time system):

int64_t convertTimePointToInt64(const std::chrono::time_point<std::chrono::system_clock> &time)
{
    auto count = time.time_since_epoch();
    auto value = std::chrono::duration_cast<std::chrono::milliseconds>(count);

    return value.count();
}

std::chrono::time_point<std::chrono::system_clock> dt = std::chrono::system_clock::now();
auto valueToStore = convertTimePointToInt64(dt);

The valueToStore is then INSERT´ed on Sqlite using SQL (INTEGER type).

My problem is when I try to read it on the Web application. I´m using the following PHP code, where $t is the database INTEGER value:

$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new \DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
$a = $d->format("Y-m-d H:i:s.u");

I´m getting stange values (1031 in year, for example) that indicates the PHP logic is messy.

The C++ code is running very fine (I can store and retrive timestamps and convert it to/back to std::string). I need to fix my PHP code to show the data correctly.

Help appreciated.

dtxzwdl08169
dtxzwdl08169 山姆,但如果我分开它会截断毫秒,或不?我试图将毫秒转换为纳秒,这是PHP所支持的......
接近 5 年之前 回复
dtwknzk3764
dtwknzk3764 如果我理解正确,你的$t的值是以毫秒为单位,然后将其转换为秒包括除以一百万,而不是乘以。如果$t在几秒钟内,则无需进行任何操作。PHP,我敢肯定,期望获得自纪元以来的秒数,而不是毫秒数。
接近 5 年之前 回复
douhao9203
douhao9203 正确。现在我注意到这是由PHP方面保存的。我会从帖子中删除它,但问题仍然是一样的......
接近 5 年之前 回复
dtftao7249656
dtftao7249656 如果数据库包含1441222475,则表示未获得毫秒数。这看起来适合传统的POSIX时间戳,因此毫秒应该需要额外的三位数。
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐