XF 2.1 Convert Date

Cupara

Well-known member
I'm currently working on pulling JSON data and the date format is
Code:
2019-11-21T23:04:52.133Z
and I want to know if there is a way to convert that to a proper format to display on my XF page where it will say XX hours ago, XX days ago, or Nov 21?
 
You can do that using something like:

PHP:
$timestamp = new \DateTime('2019-11-21T23:04:52.133Z')->getTimestamp();
// or...
$timestamp = strtotime("2019-11-21T23:04:52.133Z");
 
Kk, so that means I would have to process the JSON in the file first into a new value that I pass to my template. Thanks
 
Still fighting with this. I understand what you're saying @Jeremy P but since I'm running the foreach in the template, I have tried a few things and the last thing I thought would work, didn't.

PHP:
            $blizzPosts = \GuzzleHttp\json_decode($results->getBody(), true);
            
            $this->assertCanonicalUrl($this->buildLink('tracker'));
            
            $blizz = array();
            
            foreach($blizzPosts AS $posts)
            {
                $blizz['created_at'] = strtotime($posts['created_at']);
                $blizz['title'] = $posts['title'];
                $blizz['excerpt'] = $posts['excerpt'];
                $blizz['url'] = $posts['url'];
                
                return $blizz;
            }

Then I just pass $blizz to the template to cycle through everything but I get the error:
HTML:
TypeError: Argument 3 passed to XF\Mvc\Controller::postDispatch() must be an instance of XF\Mvc\Reply\AbstractReply, array given, called in /home/goblintimes/public_html/addons/src/XF/Mvc/Dispatcher.php on line 383 in src/XF/Mvc/Controller.php at line 281
 
[LIST=1]
[*]XF\Mvc\Controller->postDispatch() in src/XF/Mvc/Dispatcher.php at line 383
[*]XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 261
[*]XF\Mvc\Dispatcher->dispatchFromMatch() in src/XF/Mvc/Dispatcher.php at line 113
[*]XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 55
[*]XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2184
[*]XF\App->run() in src/XF.php at line 391
[*]XF::runApp() in index.php at line 20
[/LIST]
 
I found a way to use JS to convert it but it's not working even though I have the script part right but I assume it's not grabbing the $posts.created_at
HTML:
<xf:foreach loop="{$blizzPosts}" value="{$posts}">
                    <script>
                        var d = new Date($posts["created_at"]);
                        var n = d.toLocaleString();
                        document.getElementById("created_at").innerHTML = n;
                    </script>
                    <li class="block-row">
                        <h4 class="node-title">
                            <img src="/src/addons/GoblinTimes/BlizzardBlueTracker/icons/Wow.png" width="16px" /><span id="created_at"></span> -  {$posts.title}
                        </h4>
                    </li>
</xf:foreach>
 
Top Bottom