Deliver dates array from structure field

Hi
I am trying to deliver an array of dates inside structure field. My problem is that my function doesn’t return any value, and I don’t have any errors. What did I miss ?

visite:
    label: Visites
    type: structure
    entry: >
      Date : <h1>{{date}}</h1>
    fields:
      date:
        label: Date
        type: date

My function is

 <?php 
    $items = $site->find('visites-guidees')->visite()->toStructure();
    $visite = [];
    foreach($items as $item) {
        $visite[] = $item->date('Y-m-d');
    }
    ?>

What I’m looking for :
<?php $visite = array('2017-05-27, 2017-04-17, 2017-03-22, ... ');?>

What does dump($items) return?

Ouch, I just don’t know how to do it. Where do I have to write this ?

Just after you define $items… I think you are not addressing the right page, but that is guesswork, therefore I would like to see the result of dump()

And then you can do the same for $visite.

 <?php 
    $items = $site->find('visites-guidees')->visite()->toStructure();
    dump($items);
    $visite = [];
    foreach($items as $item) {
        $visite[] = $item->date('Y-m-d');
    }
    dump($visite);
?>

So I tried and then

And I have

(
[0] => Structure Object
    (
        [date] => Field Object
            (
                [page] => visites-guidees
                [key] => date
                [value] => 2017-05-25
            )

    )

[1] => Structure Object
    (
        [date] => Field Object
            (
                [page] => visites-guidees
                [key] => date
                [value] => 2017-05-26
            )

    )

)

Array
(
[0] => Y-m-d
[1] => Y-m-d
)

Ok, so that’s fine. Now dump($visite). You don’t have to echo the dump… see edit of my last post.

I replied just during you typed your response…So I edited too. My previous reply is from the code you previously edited :slight_smile:
I used

 <?php 
    $items = $site->find('visites-guidees')->visite()->toStructure();
    dump($items);
    $visite = [];
    foreach($items as $item) {
        $visite[] = $item->date('Y-m-d');
    }
    dump($visite);
?>

I can see that is not correct :

    Array
    (
    [0] => Y-m-d
    [1] => Y-m-d
    )
1 Like

Yes, that’s not correct, but that’s a bit strange. It should look like this:

Array
(
    [0] => 2017-05-25
    [1] => 2017-05-16
)

Have you set a different date handler in your config? If you have set it to strftime that might be the reason why this happens.

Yes, I have it, and all my document is running with c::set('date.handler', 'strftime');

Then you need:

$visite[] = $item->date('%G-%m-%d');

https://secure.php.net/manual/en/function.strftime.php

1 Like

Well, once again, you solved my problem, thank you very much.
Best