PHP - Blogger API的日期为毫秒

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

我试图将博客时间戳格式转换为毫秒,以下是我的代码。一旦转换为毫秒,然后我进一步将毫秒转换为日期和时间,但它显示错误的日期和时间

示例代码:

<?php 
 echo strtotime("2018-02-26T01:52:00-08:00");
?>

输出:1519638720

示例代码:

$milliseconds = 1519638720;
$seconds = $milliseconds / 1000;
$date = date("d M Y, D", $seconds);
$seconds = $milliseconds / 1000;
$time = date("g:i a", $seconds);
echo $date.", ".$time;

错误的输出:1970年1月18日,太阳,下午2:07

php datetime timestamp blogger milliseconds
2个回答
0
投票

唯一的错误,你做的是strtotime返回秒数,所以这样改进:

$seconds = strtotime("2018-02-26T01:52:00-08:00");
$date = date("d M Y, D", $seconds);
$time = date("g:i a", $seconds);

0
投票

你在评论中写道:

实际上我需要以毫秒格式存储博客发布日期。

基于此,最好的选择是让PHP版本> 7.1(而不是7.1.3,因为它有一个bug,请参阅here),其中微秒

创建新的DateTime对象:

$now = new DateTime();

要使用微秒部分使用格式类型Uu输出时间戳,请参阅here它们是什么。

$uSeconds = $now->format('U.u'); // 

现在将这些数据存储在您的数据库中。


如果你没有PHP> 7.1,那么使用microtime(true),它将返回带有微秒部分的unix时间戳。

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