0

I have a tried numerous examples on stack already and I cant seem to get this right! I am trying to pull the date from this DateTime Object also the reason for the two lines of time was because I was trying different methods and compare the return.

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

but for some reason i'm getting the completely wrong date and time.

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

I've tried the following: JSON Array to PHP DateTime PHP date time from string Arrays - foreach brings ->Fatal error: Cannot use object of type

PHP CODE:

<?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>";
    }

?>

If I echo '<pre>'; print_r($vPacCreated);

I get the correct results i want to grab

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
)

Here is the portion of the values:

 [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
                                )
3
  • 1
    It seems from your question that $vPacCreated is already a DateTime object, so you should be able to just echo $vPacCreated->format('Y-m-d H:i:s'); Commented Sep 4, 2019 at 21:49
  • 1
    You get dates like this 1970-01-01 when there is an error in the date you are trying to set Commented Sep 4, 2019 at 21:50
  • @Nick you are the man!!!!! Thank you so much that worked please set as the answer so i can accept! Commented Sep 5, 2019 at 13:35

1 Answer 1

1

Judging from your input data, it looks like $vPacCreated is already a DateTime object, so you should be able to just

echo $vPacCreated->format('Y-m-d H:i:s');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I was pulling my hair out on this one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.