0

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.

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, got it now. Cheers
No worries, glad you got it sorted! :)
Hi... I just got issue still in this case. Please have a look on my updated post
Could you please update the code snippet to be complete with the variables you are using? This will help see exactly what values are going in to the function arguments.

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.