formatting date template SS 4.0

Tags: #<Tag:0x00007f0827118ec0> #<Tag:0x00007f0827118998>


Silverstripe Version: 4.2.2

Hello All!
I 've search in API 4.0, but can’t find an answer… :frowning_face:
i have a dB date field type in my SS_silverstripe data base using MariaDB.
i have un array and push the ‘mDates’=>$row[‘Date’]. That’s right and OK.
So, in the template, if i use $mDates, i have the good return, i see the good Date.

But, there is a but…
when i try to use in the template format expression like this (new (CLDR format), $mDates.Format(‘dd.MM.y’), i have un empty return… Please Doctor, where’s my problem?

Thank’s a lot for your return :wink:

		new ArrayData(array(
<%-- ***SAMPLE DATE FORMAT*** --%>
<div class="fatDate">
       <span class="dates">$mDates.Format('dd.MM.y')</span>
	<span class="dayOfMonth">$mDates.Format('dd)</span>
	<span class="month">$mDates.Format('MM')</span>
	<span class="year">$mDates.Format('y')</span>


Most likely, the reason you’re not getting anything is that in the course of the process, you’re pushing a string into the arraydata. In order to be able to use the formatting in a template you need a date object in there.

Can you expand on your code a little more? Is there a reason you’re manually creating the ArrayData? How are you getting the data from the database?


hey DorsetDigital! thank’s to you for you’r interest!
I understant what’s you said, and thinking like you. And i trying to test with the SetDateFormat.
So In my error.log:
[2018-12-24 11:47:38] error-log.ERROR: Uncaught Exception Error: “Call to a member function setDateFormat() on string” at /var/www/silverstripe/app/src/Page.php line 116 {“exception”:"[object] (Error(code: 0): Call to a member function setDateFormat() on string at /var/www/silverstripe/app/src/Page.php:116)"}

So i found this solution, but it’s not realy sexy…

in my dB this is the table ‘MediaPage’:

public function RecentJeunes()
		$list1 = ArrayList::create();
		$sqlQuery = new SQLSelect();
 	       $sqlQuery->addInnerJoin('MediaPage_Categories','"MediaPage_Categories"."MediaPageID" = "MediaPage"."ID"');
		$sqlQuery->addWhere('"MediaPage_Categories"."MediaTagID" = 1');
		$sqlQuery->setOrderBy('Date', 'DESC');
		// $sqlQuery->setGroupBy(...);
		// $sqlQuery->setHaving(...);
		// Execute and return a Query object
		$results = $sqlQuery->execute();
		// Iterate over results
		foreach($results as $row) {
			$type = SiteTree::get()->byID($row['MediaPageID']);
			if ($type) { 
				$name = $type->title;
			} else {
				new ArrayData(array(
		return $list1;
		<span class="title">Jeunes</span>
		<% loop $RecentJeunes %>
				<%-- p class="infos">$Title - $Date - $Abstract.LimitWordCount(10)</p --%>
				<div class="event">
					<div class="fatDate">
						<span class="dayOfMonth">$mDatesD</span>
						<span class="month">$mDatesM</span>
						<span class="year">$mDatesY</span>
					<div class="fatText">
				<a href="$InternalLink" class="readMore"> >> Read More...</a>
		<% end_loop %>

if you can tel me more, i realy appreciate!



Is there a reason you’re not using the ORM for these queries? Having queries nested inside a foreach loop has the potential for some serious performance issues.

In theory you should be able to replace all that code with a relatively simple one-liner (assuming you have the relation set up between MediaPage and MediaPage_Categories) :

public function getRecentJeunes() {
  return  MediaPage::get()->filter(['MediaPage_Categories.MediaTagID' => 1])->Limit(3);

Then in your template, you can use the format() methods, eg: $Date.Format('MM')