WEBVTT NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com 00:00:01.066 --> 00:00:03.026 align:middle Let's make this date dynamic! 00:00:03.436 --> 00:00:10.636 align:middle The field on Question that we're going to use is $askedAt, which - remember - might be null. 00:00:11.616 --> 00:00:16.416 align:middle If a Question hasn't been published yet, then it won't have an askedAt. 00:00:17.336 --> 00:00:18.206 align:middle Let's plan for this. 00:00:18.496 --> 00:00:26.096 align:middle In the template, add {% if question.askedAt %} with an {% else %} and {% endif %}. 00:00:26.096 --> 00:00:29.756 align:middle If the question is not published, say (unpublished). 00:00:30.806 --> 00:00:36.086 align:middle In a real app, we would probably not allow users to see unpublished questions... 00:00:36.436 --> 00:00:39.126 align:middle we could do that in our controller by checking for this field 00:00:39.306 --> 00:00:43.916 align:middle and saying throw $this->createNotFoundException() if it's null. 00:00:44.766 --> 00:00:49.896 align:middle But... maybe a user will be able to preview their own unpublished questions. 00:00:50.596 --> 00:00:53.126 align:middle If they did, we'll show unpublished. 00:00:54.226 --> 00:01:00.566 align:middle The easiest way to try to print the date would be to say {{ question.askedAt }}. 00:01:01.626 --> 00:01:04.156 align:middle But... you might be shouting: "Hey Ryan! 00:01:04.296 --> 00:01:05.466 align:middle That's not going to work!". 00:01:06.186 --> 00:01:12.676 align:middle And... you're right: Object of class DateTime could not be converted to string We know 00:01:12.676 --> 00:01:19.186 align:middle that when we have a datetime type in Doctrine, it's stored in PHP as a DateTime object. 00:01:19.536 --> 00:01:23.166 align:middle That's nice because DateTime objects are easy to work with... 00:01:23.496 --> 00:01:25.596 align:middle but we can't simply print them. 00:01:25.596 --> 00:01:30.716 align:middle To fix this, pass the DateTime object through a |date() filter. 00:01:31.466 --> 00:01:37.326 align:middle This takes a format argument - something like Y-m-d H:i:s. 00:01:37.326 --> 00:01:40.136 align:middle When we try the page now... 00:01:40.476 --> 00:01:42.716 align:middle it's technically correct... 00:01:42.876 --> 00:01:44.246 align:middle but yikes! 00:01:45.166 --> 00:01:46.106 align:middle This... well... 00:01:46.396 --> 00:01:51.446 align:middle how can I put this politely: it looks like a backend developer designed this. 00:01:52.236 --> 00:01:55.076 align:middle Whenever I render dates, I like to make them relative. 00:01:55.166 --> 00:02:01.096 align:middle Instead of printing an exact date, I prefer something like "10 minutes ago". 00:02:01.906 --> 00:02:04.336 align:middle It also avoids timezone problems... 00:02:04.666 --> 00:02:07.426 align:middle because 10 minutes ago makes sense to everyone! 00:02:08.066 --> 00:02:12.586 align:middle But this exact date would really need a timezone to make sense. 00:02:13.566 --> 00:02:14.466 align:middle So let's do this. 00:02:14.856 --> 00:02:17.936 align:middle Start by adding the word "Asked" back before the date. 00:02:19.356 --> 00:02:24.606 align:middle Cool. To convert the DateTime into a friendly string, we can install a nice bundle. 00:02:25.166 --> 00:02:32.626 align:middle At your terminal, run: composer require knplabs/knp-time-bundle You could find this 00:02:32.626 --> 00:02:35.386 align:middle bundle if you googled for "Symfony ago". 00:02:36.336 --> 00:02:41.876 align:middle As we know, the main thing that a bundle gives us is more services. 00:02:42.826 --> 00:02:50.016 align:middle In this case, the bundle gives us one main service that provides a Twig filter called ago. 00:02:50.616 --> 00:02:51.376 align:middle It's pretty awesome. 00:02:52.096 --> 00:02:54.746 align:middle Back in the template, add |ago. 00:02:55.436 --> 00:02:55.976 align:middle We're done! 00:02:56.496 --> 00:02:57.796 align:middle When we refresh now... 00:02:57.796 --> 00:03:06.126 align:middle woohoo! Asked 1 month ago Next: let's make the homepage dynamic by querying for all 00:03:06.126 --> 00:03:08.676 align:middle of the questions in the database and rendering them. 00:03:09.186 --> 00:03:14.216 align:middle Along the way, we're going to learn a secret about the repository object.