3

I have just discovered the following library https://github.com/simshaun/recurr and the output of is using a Doctrine arrayCollection.

How can I use a foreach loop to loop through this array and get the date value?

Array
 (
    [0] => Recurr\Recurrence Object
        (
        [start:protected] => DateTime Object
            (
                [date] => 2014-08-03 15:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

        [end:protected] => DateTime Object
            (
                [date] => 2014-08-03 17:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

    )

[1] => Recurr\Recurrence Object
    (
        [start:protected] => DateTime Object
            (
                [date] => 2014-08-04 15:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

        [end:protected] => DateTime Object
            (
                [date] => 2014-08-04 17:00:00.000000
                [timezone_type] => 3
                [timezone] => America/Vancouver
            )

    )

  )
2
  • Please, be more specific: what do you want to do here? Commented Nov 7, 2014 at 12:02
  • Apologies, updated my question. I am trying to get the date value. Commented Nov 7, 2014 at 12:15

1 Answer 1

3

This will not work in this context - you can use a foreach loop to get through the elements, but you are not allowed to access the property of the object, because it's marked as protected. So you'll use a Getter that you can access it.

foreach (<yourarray> as $numObject => $object)
{
    $object->end;  // So you could access it, but its protected
    $object->getEndDate();  // Like this you can access it
}

And if you have it, than you have a simple \DateTime Object and with the format method you can get your date string e.g. $object->getEndDate()->format('Y-m-d H:i:s');.

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

2 Comments

Hey andreashager, I believe I understand exactly what you are saying. I've tried and for some reason I get the following error "Call to undefined method Recurr\Recurrence::getEndDate() i"
Small typo.. thanks for your help.. print_r($object->getStart()->format('Y-m-d H:i:s')); was what I needed.

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.