2

I have been struggling with traversing php's objects. I have the following DateTime object in an array, but am having trouble iterating through it. What am I doing wrong?

// print_r($calendar_days);
Array
(
    [0] => DateTime Object
        (
            [date] => 2016-04-06 00:00:00
            [timezone_type] => 3
            [timezone] => Europe/Helsinki
        )

    [1] => DateTime Object
        (
            [date] => 2016-04-13 00:00:00
            [timezone_type] => 3
            [timezone] => Europe/Helsinki
        )

    [2] => DateTime Object
        (
            [date] => 2016-04-20 00:00:00
            [timezone_type] => 3
            [timezone] => Europe/Helsinki
        )

    [3] => DateTime Object
        (
            [date] => 2016-04-27 00:00:00
            [timezone_type] => 3
            [timezone] => Europe/Helsinki
        )

)

PHP

<?php foreach ($calendar_days as $key => $value){ ?>
  <a href="#">
    <div class="day-container">
      <strong> <?php echo $key->date; ?></strong>
    </div>
  </a>
<?php } 

Thanks.

3
  • it should be $value->date;. Commented Apr 4, 2016 at 9:45
  • I tried that, but got Message: Undefined property: DateTime::$date Commented Apr 4, 2016 at 9:47
  • 1
    <?php echo $value->format('Y-m-d H:i:s'); ?> Commented Apr 4, 2016 at 9:55

1 Answer 1

1

You can find an example ideone here.

Essentially your current code is trying to access the member variable date on the $key of the array, so it is trying to do something like;

echo 0->date;

PHP doesn't understand exactly what you mean, so you won't be getting the output you expect. What you want to do is access the value (the actual DateTime object) and use that to output the dates instead, like so;

echo $value->format('Y-m-d H:i:s');

You need to remember you're handling DateTime objects, so just trying to access ->date won't work as you expect. To my knowledge the DateTime object does not allow you to directly access its properties and instead if you want to print out the date you have to use it's public functions instead ->format()

You can find the full docs for DateTime here

Edit: As mentioned @barat, you will no longer need $key => $value now that you are not trying to access the $key so you can omit $key:

foreach ($calendar_days as $value) { ... }

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

4 Comments

BTW ... if You're iterating through Array and don't need keys just do it like: foreach($calendar_days as $date) {echo $date->format('Y-m-d H:i:s');}
Aye you don't, but since he had it originally I thought I'd just leave it in to leave the code closer to the original, and just in case he had some other reason for using the $key
My comment was more to user3442612 - I'm sure that for You it's common knowlage :)
Ohhhhh, the value is an object. I feel kind of dumb now. Thanks for the clear explanation.

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.