Meddysong Posted April 23, 2019 Posted April 23, 2019 Hello! I'm out of my depth, same as usual. I've pulled some data from an API and have returned my array of objects. I can display the date easily enough with the following: {{foreach $result as $res}} <p>{$res['date']}</p> {{endforeach}} The dates take the form yyyy-mm-dd. For styling purposes, what I'd like to do is be able to display the day from that day and then in another line of code the month, maybe even the year as a third. My problem is that I don't know how to do this is. Ifย <p>{$res['date']}</p> returns 2019-04-23, how do I change it to display i) 23, ii) Apr, iii) 2019?
opentype Posted April 23, 2019 Posted April 23, 2019 When the API doesnโt provide the pieces, you can โexplodeโ it with the hyphen as delimiter.ย https://www.php.net/manual/en/function.explode.php
Mark Posted April 23, 2019 Posted April 23, 2019 Or if you do... $timestamp = strtotime( $res['date'] ); ... then you can do... date( 'j', $timestamp ) // Day date( 'n', $timestamp ) // Month date( 'Y', $timestamp ) // Year Seeย https://www.php.net/manual/en/function.date.phpย for other values you can use instead of j/n/Y Or if this is within Invision Community you can do... {datetime="strtotime( $res['date'] )"} To get the "relative" date
Meddysong Posted April 23, 2019 Author Posted April 23, 2019 Oh, oh, I've got it working ... and hit upon a problem which I hadn't anticipated.ย The PHP formats return values for %D, %M etc in English, don't they? Bother. I suspect one solution might be for me to write some little formula to look up a value corresponding to the output of, say, %n (the monthย number, ranging from 1 toย 12). Or is there some tool already built within the site? I've noticed that the upcoming events widget displaysย values according to the locale so thought I'd have a look atย upcomingEvents.php. Unfortunately, that doesn't contain any of the HTML to be output, so I wasn't able to see what function produces the month name which is formatted by .ipsCalendarDate_month when the widget loads.
bfarber Posted April 23, 2019 Posted April 23, 2019 So, looking at your code this appears to be within the Invision Community software, in a template. I assume the language you wish to output the dates as is installed and you have the language pack available, and selected by your user account. In that case, you can use our \IPS\DateTime class {{$dateTime = \IPS\DateTime::ts( strtotime( $res['date'] ) );}} {$dateTime->strFormat( '%D' )} https://invisioncommunity.com/developers/docs/fundamentals/dates-and-times-r165/
Meddysong Posted April 23, 2019 Author Posted April 23, 2019 Thank you for taking the time to look into this and advise me, Brandon. Unfortunately, it's not generating expected results. My template includes the following: {{$timestamp = strtotime( $res['komenca_dato'] );}} {{$tago = date( 'j', $timestamp );}} {{$monato = date( 'M', $timestamp );}} {{$jaro = date( 'y', $timestamp );}} {{$dateTime = \IPS\DateTime::ts( strtotime( $res['komenca_dato'] ) );}} If I have <time datetime="{$res['komenca_dato']}" class="ipsCalendarDate"> <span class="ipsCalendarDate_month" data-format="%M">{$monato}</span> <span class="ipsCalendarDate_date" data-format="%j">{$tago}</span> </time> then I correctly see when $res['komenca_dato'] isย 2019-05-03. Since the month name is in English, I instead try <time datetime="{$res['komenca_dato']}" class="ipsCalendarDate"> <span class="ipsCalendarDate_month" data-format="%M">{$dateTime->strFormat( '%M' )}</span> <span class="ipsCalendarDate_date" data-format="%j">{$dateTime->strFormat( '%j' )}</span> <!-- I don't really need this because the result was correct originally, but it's here for demonstration purposes --> </time> and get Every return has a month '00' instead of the name of the month in my language. The '123' seems to be because May 3rd is the 123rd day of the year, although the PHP docs show that %j should return a figure between 1 and 7. It's not the end of the world. Thank you for sparing me some of your time to help.
newbie LAC Posted April 24, 2019 Posted April 24, 2019 Hello, Example #1 {{$tago = date( 'j', $timestamp );}} {{$monato = date( 'M', $timestamp );}} {{$jaro = date( 'y', $timestamp );}} You useย https://www.php.net/manual/en/function.date.php jย ย ย Day of the month without leading zerosย ย ย ย ย ย ย ย ย ย ย ย ย ย ย 1ย toย 31 Mย ย ย A short textual representation of a month, three lettersย ย ย Jan through Dec yย ย ย A two digit representation of a yearย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย Examples: 99 or 03 Example #2 <span class="ipsCalendarDate_month" data-format="%M">{$dateTime->strFormat( '%M' )}</span> <span class="ipsCalendarDate_date" data-format="%j">{$dateTime->strFormat( '%j' )}</span> <!-- I don't really need this because the result was correct originally, but it's here for demonstration purposes --> You useย strFormat() /** * Format times based on strftime() calls instead of date() calls, and convert to UTF-8 if necessary * * @param string $format Format accepted by strftime() * @param \IPS\Lang|\IPS\Member|NULL $memberOrLanguage The language or member to use, or NULL for currently logged in member * @return string */ https://www.php.net/manual/en/function.strftime.php Mย ย ย Two digit representation of the minuteย ย ย ย ย ย ย 00ย throughย 59 jย ย ย ย Day of the year, 3 digits with leading zerosย ย ย ย 001ย toย 366
Meddysong Posted April 24, 2019 Author Posted April 24, 2019 Oh, that's so obvious now that you've pointed it out! Thank you! Unfortunately, it hasn't actually solved the real problem. Here's a screenshot from what I'm programming based on the API: Note that the month name is still in English. I know that the locale is installed and loaded because if I use the upcoming events widget, the month is no longer in English but is in my language:
Meddysong Posted April 24, 2019 Author Posted April 24, 2019 1 hour ago, newbie LAC said: Do you use the first code? No, I'm using the second: <div class="datoj">{{$komenca_dato = \IPS\DateTime::ts( strtotime( $res['komenca_dato'] ) );}} <time datetime="{$komenca_dato}" class="ipsCalendarDate"> <span class="ipsCalendarDate_month" data-format="%b">{$komenca_dato->strFormat( '%b' )}</span> <span class="ipsCalendarDate_date" data-format="%e">{$komenca_dato->strFormat( '%e' )}</span> </time> </div> ย
newbie LAC Posted April 24, 2019 Posted April 24, 2019 4 hours ago, Meddysong said: because if I use the upcoming events widget, the month is no longer in English but is in my language: That widget uses theย core.global.core.datetime controller exampleย <span class='ipsCalendarDate_month' data-controller="core.global.core.datetime" data-time="{$event->nextOccurrence( $today, 'startDate' )->format('c')}" data-format="%b">{$event->nextOccurrence( $today, 'startDate' )->monthNameShort}</span> the next part {$event->nextOccurrence( $today, 'startDate' )->monthNameShort} is replaced byย javascript and the month names gets from language 'month_0' => "January", 'month_1' => "February", 'month_2' => "March", 'month_3' => "April", 'month_4' => "May", 'month_5' => "June", 'month_6' => "July", 'month_7' => "August", 'month_8' => "September", 'month_9' => "October", 'month_10' => "November", 'month_11' => "December", 'month_0_short' => "Jan", 'month_1_short' => "Feb", 'month_2_short' => "Mar", 'month_3_short' => "Apr", 'month_4_short' => "May", 'month_5_short' => "Jun", 'month_6_short' => "Jul", 'month_7_short' => "Aug", 'month_8_short' => "Sep", 'month_9_short' => "Oct", 'month_10_short' => "Nov", 'month_11_short' => "Dec", See how it works. Screenshots from your forum Javascript enabled Javascript disabled
Meddysong Posted April 24, 2019 Author Posted April 24, 2019 I'm not sure about how to do it. I know I can add the controller but then I get NaN. What do I need to generate? A language string month_{$komenca_dato->strFormat( '%m' )} ? The problem with that is that strFormat seems only capable of providing two-digit months, so I would be able to get month_01 but but not month_1. (Plus the values aren't accurate; January is the 0th month in the JS array.) How can I do it? My variable $komenca_dato doesn't have the property monthNameShort. ย
newbie LAC Posted April 24, 2019 Posted April 24, 2019 You should to add the data-time attribute <div class="datoj">{{$komenca_dato = \IPS\DateTime::ts( strtotime( $res['komenca_dato'] ) );}} <time datetime="{$komenca_dato}" class="ipsCalendarDate"> <span data-controller="core.global.core.datetime" class="ipsCalendarDate_month" data-time="{$res['komenca_dato']}" data-format="%b">{$komenca_dato->strFormat( '%b' )}</span> <span data-controller="core.global.core.datetime" class="ipsCalendarDate_date" data-time="{$res['komenca_dato']}" data-format="%e">{$komenca_dato->strFormat( '%e' )}</span> </time> </div>
Meddysong Posted April 24, 2019 Author Posted April 24, 2019 Amazing -- it works! Thank you for all your help ๐
Meddysong Posted April 30, 2019 Author Posted April 30, 2019 Thank you for your help, @opentype, @Mark, @bfarberย and, of course, @newbie LAC. With your help, I've been able to take an api from here and turn it into something pretty here. I'm really happy with the result ๐
Recommended Posts
Archived
This topic is now archived and is closed to further replies.