1

Returning data from the database to twig, I dumped the array in twig and I see this

array (size=1)
'transactions' => 
 array (size=150)
  0 => 
    array (size=12)
      'id' => int 391
      'trans_id' => string '294661627' (length=9)
      'pay_amount' => string '40.00' (length=5)
      'account_code' => string '874996' (length=6)
      'pid' => int 726
      'chrg_date' => 
    object(DateTime)[825]
      public 'date' => string '2019-11-19 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'America/New_York' (length=16)
     'processed' => string 'EXACT' (length=5)
     'process_date' => 
    object(DateTime)[821]
      public 'date' => string '2019-11-27 12:38:11.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'America/New_York' (length=16)
      'encounter' => int 5287
      'lname' => string 'Morris' (length=6)
      'fname' => string 'Peter' (length=5)
      'email' => string '' (length=0)
  1 => 

I have two date objects in the array. So, in the template I use

            <td>{{ transaction.chrg_date | date('Y-m-d') }}</td>

The data in the table is properly formated.

database search for the date-time stamp

However, the dates are displayed like this:

output to the screen

Notice how all the dates are the same. I checked the database and this is not the data that is in the table.

What is the best way to access the date object in this array?

I found this post How to render a DateTime object in a Twig template.

None of the examples I have followed have worked.

As a final thought, I went back to my entity and looked at the getter method and it is this.

public function getChrgDate()
{
    return $this->chrg_date;
}
user1794918
  • 1,131
  • 2
  • 16
  • 34
  • I'm not sure but I think the date filter is only used when your twig variable is a timestamp. But looks like you already have a date well formatted in the database. I would have simply tried to remove the date filter. – Minirock Dec 05 '19 at 14:11
  • When I remove the date filter and use {{ transaction.chrg_date.date }}. This is what is returned to the screen. -0001-11-30 00:00:00.000000. As you can see the year is jacked up. But besides that, every date displayed is exactly the same. – user1794918 Dec 05 '19 at 14:18
  • 1
    What happens when you try `{{ "now"|date("m/d/Y") }}` ? – Quisse Dec 05 '19 at 14:44
  • Your data dump does not reflect the data passed to twig - It's seem you are storing the account code in `chrg_date` (`-0001-11-30`). Please verify your data – DarkBee Dec 05 '19 at 14:58
  • 1
    @Minirock twig will convert any valid datetime notation to a datatime object – DarkBee Dec 05 '19 at 15:02
  • When I try "now"|date("m/d/Y"). It shows todays date 293051454 40.00 151876 12/05/2019 EXACT – user1794918 Dec 05 '19 at 15:04
  • I did see the fields were incorrectly marked and I moved the headers. Thx As I keep reviewing the data displayed. I noticed that some of the dates are correctly displayed in the charge column and some are not. I did check the database again to make sure that none of the date in the table began with -0001. – user1794918 Dec 05 '19 at 15:06
  • Thank you all for your help. There is nothing wrong with Twig. It is doing its job properly. It is the data in the database. Where the date is 0000-00-00 00:00:00. Twig compensates in some way for it being zeros. That is why it comes out -0001. There is no year there. I need to add and if statement to check the value before attempting to display zeros. – user1794918 Dec 05 '19 at 15:19
  • It is Doctrine that is returning the date -0001-11-30 – user1794918 Dec 05 '19 at 15:26
  • I have been wondering should I delete this post. What do you guys say? – user1794918 Dec 05 '19 at 21:21

0 Answers0