HiDayurie Dave
HiDayurie Dave

Reputation: 1807

PHP get sub value of array

I have this array value:

Array
(
    [0] => DateTime Object
        (
            [date] => 2019-01-28 08:19:44
            [timezone_type] => 3
            [timezone] => Asia/Jakarta
        )
)

And PHP:

for($i = 0; $i < $rowsintimespanAccept; $i++)
{
    $a = array();

    $a[] = DateTime::createFromFormat($createFromFormat, $linesAccept[$i]);
    if(count(array_filter($a)) == count($a))
    {
        echo "<pre>";
        print_r($a);
    }
}

Now my question how to get [date] value?

I tried using this code but got empty.

print_r($a['date']);

UPDATED

Now I have this array value:

DateTime Object
(
    [date] => 2019-01-28 08:21:01
    [timezone_type] => 3
    [timezone] => Asia/Jakarta
)
DateTime Object
(
    [date] => 2019-01-28 08:21:14
    [timezone_type] => 3
    [timezone] => Asia/Jakarta
)
DateTime Object
(
    [date] => 2019-01-28 08:19:44
    [timezone_type] => 3
    [timezone] => Asia/Jakarta
)

and PHP:

for($i = $readRowAfter; $i < count($linesAccept); $i++)
{
    $dateobjAccept = DateTime::createFromFormat($createFromFormat, $linesAccept[$i]);

    if($dateobjAccept < $toDateTime && $dateobjAccept > $fromDateTime)
    {
        $rowsintimespanAccept++;
    }

    echo "<pre>";
    print_r($dateobjAccept);
}

Tried using this:

print_r($dateobjAccept->format('Y-m-j H:i:s'));

But only got result: 2.

Upvotes: 0

Views: 111

Answers (1)

jaseeey
jaseeey

Reputation: 281

Using print_r() to see inside the DateTime object outputs it like an array, but because it's an object, you can't simply access it like an array.

You can extract a date in any given format using DateTime::format(), so in your case $a[0]->format('Y-M-j H:i:s') will display the date/time in the same format as you're seeing in the print_r() output.

Source: Why can't I access DateTime->date in PHP's DateTime class? Is it a bug?

Upvotes: 1

Related Questions