繁体   English   中英

从JSON到PHP Foreach循环的日期/时间对象

[英]Date/Time Object from JSON to PHP Foreach Loop

我已经在堆栈上尝试了许多示例,但我似乎无法正确解决这个问题! 我试图从该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数组到PHP DateTime 从字符串 数组 到PHP 日期时间 -foreach带来->致命错误:无法使用类型的对象

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 '<pre>'; print_r($vPacCreated); echo '<pre>'; 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->format('Y-m-d H:i:s');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM