I have a nested custom date field is it possible change the way the date is formatted in my template?
<?php foreach($articles as $article): ?>
<a href="">
<div class="left"><?php echo $article['title'] ?></div>
<div class="middle"><?php echo $article['publisher'] ?></div>
<div class="right"><?php echo $article['date'] ?></div>
</a>
<?php endforeach ?>
What format do you want the date in?
short month and year e.g Oct 2017
Something like this should probably work (untested, but borrowed code from one or my sites)
<?php foreach($articles as $article): ?>
<?php $dateformat = date('M-Y', strtotime($article['date']));?>
<a href="">
<div class="left"><?= $article['title'] ?></div>
<div class="middle"><?= $article['publisher'] ?></div>
<div class="right"><?= $dateformat ?></div>
</a>
<?php endforeach ?>
Refer to PHP date formatting here
1 Like
If you use toStructure()
instead of yaml()
on your structure field, it’s a bit more comfortable, because you can then use the date method with a format parameter.
$article->date('M-Y');
1 Like
ah! thank you both, toStructure() is new to me 
<? php foreach($page->articles()->toStructure() as $article): ?>
<a href="">
<div class="left"><?php echo $article->title() ?></div>
<div class="middle"><?php echo $article->publisher() ?></div>
<div class="right"><?php echo $article->date('M Y')?></div>
</a>
<?php endforeach ?>
No worries, should be date('M-Y')
though.
and <?=
is the same as <?php echo
… saves you some typing 