1

I'm trying to get data out of JIRA and into a table. I have got the API request working and I have got almost all the data out of JIRA and into the table that I need, I just can't seem to get the created date out as it is in a datetime object within the array and doesn't have a proper format.

    foreach ($ret->issues as $issue)
    {
            $date = new DateTime($issue->fields->created->date);
            $date = $date->format('Y-m-d');
        echo '<tr>
                <td>'.$issue->fields->status->name.'</td>
                <td>'.$date.'</td>
                <td>'.$issue->fields->priority->name.'</td>
                <td>'.$issue->fields->summary.'</td>
                <td>'.$issue->fields->assignee->displayName.'</td>
                </tr>';
    }

That is the PHP code for building the table, as you can see I've tried to format it with DateTime.

Here is the array if I die on $issue->fields->created

    object(DateTime)#318 (3) {
        ["date"]=>
        string(26) "2018-10-22 12:47:02.000000"
        ["timezone_type"]=>
        int(1)
        ["timezone"]=>
        string(6) "-04:00"
      }

I just can't seem to get that data out as $issue->fields->created->date shows me:

PHP Fatal error:  Cannot use object of type DateTime as array

Which is why I tried to format it as above. Now I get:

PHP Notice:  Undefined property: DateTime::$date

Any ideas?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Pete Wall
  • 55
  • 11

1 Answers1

1

$issue->fields->created is already a DateTime object. To format that as Y-m-d format, you can simply call format() on that directly:

echo '<tr>
    <td>'.$issue->fields->status->name.'</td>
    <td>'.$issue->fields->created->format('Y-m-d').'</td>
    <td>'.$issue->fields->priority->name.'</td>
    <td>'.$issue->fields->summary.'</td>
    <td>'.$issue->fields->assignee->displayName.'</td>
</tr>';

If you want to convert it to a different timezone, you can do that as well:

$issue->fields->created->setTimezone(new DateTimeZone("UTC"))->format('Y-m-d')
rickdenhaan
  • 10,857
  • 28
  • 37