【问题标题】:Date/Time Object from JSON to PHP Foreach Loop从 JSON 到 PHP Foreach 循环的日期/时间对象
【发布时间】:2020-01-07 18:26:25
【问题描述】:

我已经在堆栈上尝试了许多示例,但我似乎无法做到这一点!我试图从这个 DateTime 对象中提取日期,这两条时间线的原因是因为我尝试了不同的方法并比较了返回。

[created] => DateTime Object
                (
                    [date] => 2019-06-06 15:22:25.720000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

但由于某种原因,我得到了完全错误的日期和时间。

19-09-04 22:37:18
1970-01-01 01:00:00

19-09-04 22:37:18
1970-01-01 01:00:00

19-09-04 22:37:18
1970-01-01 01:00:00

19-09-04 22:37:18
1970-01-01 01:00:00

我尝试了以下方法: JSON Array to PHP DateTime PHP date time from string Arrays - foreach brings ->Fatal error: Cannot use object of type

PHP 代码:

<?php 
    $vPaccomments = $issue->fields->comment->comments;

    foreach ($vPaccomments as $vPaccomment) {
        $vPacAvatarUrl = $vPaccomment->author->avatarUrls;
        $size16 = "16x16";
        $vPacCreated = $vPaccomment->created;
        $vPacDate = $vPacCreated->date;
        $vPacDateConv = date('Y-m-d H:i:s',strtotime($vPacDate));

        $dateTime = new DateTime($vPacDate);
        echo $dateTime->format('y-m-d H:i:s');

        echo '<pre>'; print_r($vPacDateConv);

        echo "<div class=\"col\">";
        //echo "<img width=\"16px\" src=\"" . $vPacAvatarUrl[$size16] . "\"> " . "<font color=\"#0015ff\">" .$vPaccomment->author->displayName ."</font> added a comment - " . date_format($vPacCreateDate, 'Y-m-d H:i:s');    
        echo "<br>";
        echo "<hr>";
        echo "</div>";
    }

?>

如果我echo '&lt;pre&gt;'; print_r($vPacCreated);

我得到了我想要获取的正确结果

DateTime Object
(
    [date] => 2019-06-06 15:22:25.720000
    [timezone_type] => 1
    [timezone] => +00:00
)

DateTime Object
(
    [date] => 2019-06-07 13:58:31.970000
    [timezone_type] => 1
    [timezone] => +00:00
)

DateTime Object
(
    [date] => 2019-06-17 14:07:23.040000
    [timezone_type] => 1
    [timezone] => +00:00
)

DateTime Object
(
    [date] => 2019-06-17 14:25:13.840000
    [timezone_type] => 1
    [timezone] => +00:00
)

这是值的一部分:

 [comment] => JiraRestApi\Issue\Comments Object
        (
            [startAt] => 0
            [maxResults] => 4
            [total] => 4
            [comments] => Array
                (
                    [0] => JiraRestApi\Issue\Comment Object
                        (
                            [self] => https://xxx/rest/api/2/xxx
                            [id] => xxx
                            [author] => JiraRestApi\Issue\Reporter Object
                                (
                                    [self] => https://xxx/rest/api/2/xxx
                                    [name] => xxx
                                    [emailAddress] => xxx
                                    [avatarUrls] => Array
                                        (
                                            [48x48] => https://xxx/secure/useravatar?avatarId=xxx
                                            [24x24] => https://xxx/secure/useravatar?size=small&avatarId=xxx
                                            [16x16] => https://xxx/secure/useravatar?size=xsmall&avatarId=xxx
                                            [32x32] => https://xxx/secure/useravatar?size=medium&avatarId=xxx
                                        )

                                    [displayName] => xxx
                                    [active] => 1
                                    [wantUnassigned:JiraRestApi\Issue\Reporter:private] => 
                                    [accountId] => 
                                    [key] => xxx
                                    [timeZone] => America/New_York
                                )

                            [body] => xxx

CREATED ON:6/6/2019

COPIED TO:\\xxx
                            [updateAuthor] => JiraRestApi\Issue\Reporter Object
                                (
                                    [self] => https://xxx/rest/api/2/xxx
                                    [name] => xxx
                                    [emailAddress] => xxx
                                    [avatarUrls] => Array
                                        (
                                            [48x48] => https://xxx/secure/useravatar?avatarId=xxx
                                            [24x24] => https://xxx/secure/useravatar?size=small&avatarId=xxx
                                            [16x16] => https://xxx/secure/useravatar?size=xsmall&avatarId=xxx
                                            [32x32] => https://xxx/secure/useravatar?size=medium&avatarId=xxx
                                        )

                                    [displayName] => xxx
                                    [active] => 1
                                    [wantUnassigned:JiraRestApi\Issue\Reporter:private] => 
                                    [accountId] => 
                                    [key] => xxx
                                    [timeZone] => America/New_York
                                )

                            [created] => DateTime Object
                                (
                                    [date] => 2019-06-06 15:22:25.720000
                                    [timezone_type] => 1
                                    [timezone] => +00:00
                                )

【问题讨论】:

  • 从你的问题看来,$vPacCreated 已经是一个DateTime 对象,所以你应该可以只使用echo $vPacCreated-&gt;format('Y-m-d H:i:s');
  • 当您尝试设置的日期出现错误时,您会得到这样的日期1970-01-01
  • @Nick 你就是那个男人!!!!非常感谢您的工作,请设置为答案,以便我接受!

标签: php arrays json datetime


【解决方案1】:

从您输入的数据来看,$vPacCreated 似乎已经是一个DateTime 对象,所以您应该可以只是

echo $vPacCreated->format('Y-m-d H:i:s');

【讨论】:

  • 谢谢我把头发拉出来了!
  • @demo7up 不用担心。我很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2011-03-21
  • 1970-01-01
  • 2015-07-01
  • 1970-01-01
相关资源
最近更新 更多