1

I am using Symfony 4 and a Database in which my information is stored. I retrieve them with a Fetch in JavaScript. In my Entity, I have an object of type "dateinterval". In the Database, for an item I have + P00Y00M14DT00H00M00S. When I get that in JavaScript, I have the value P0Y0M14DT0H0M0S. I want to recover the value of the year of the month and days. I tried with substr (5, 2) but when my value of days is null it contains only one digit: P0Y1M0DT0H0M0S and it does not work.

In my Entity

    /**
     * @ORM\Column(type="dateinterval")
     * @Groups({"read"})
     */
    private $duration;

In my FormType

 ->add('duration', DateIntervalType::class, [
                'widget' => 'choice',
                'with_years' => true,
                'with_months' => true,
                'with_days' => false,
                'with_weeks' => true,
                'placeholder' => [
                    'years' => 'Années',
                    'months' => 'Mois',
                    'weeks' => 'Semaines'
                ],
                'labels' => [
                    'years' => 'Années',
                    'months' => 'Mois',
                    'weeks' => 'Semaines'
                ]
            ])

In the Database

+P00Y01M00DT00H00M00S
+P00Y00M14DT00H00M00S
+P00Y00M63DT00H00M00S

In my Fetch JavaScript, the values received:

P0Y1M0DT0H0M0S
P0Y0M14DT0H0M0S
P0Y0M63DT0H0M0S
  • Detect the index of the letters ( M and D ) and then use all the numbers until the index of the previous letter so that it does not matter if there's 0 or 25 numbers between the letters. A RegExp can do this as well. – Shilly May 08 '19 at 12:58
  • `'P0Y0M14DT0H0M0S'.split(/[A-Z]{1,2}/g)[2]` – Yevhen Horbunkov May 08 '19 at 13:05
  • I tested with my variable: 'P0Y0M14DT0H0M0S'.split (/ [A-Z] {1,2} / g) [2] It's perfect, it works I can recover all my values. Thanks you very much for your help! – Elodie Martin May 08 '19 at 13:28

0 Answers0