0

As per http://php.net/manual/en/class.datetime.php

PHP follows "ATOM" date format. To produce a date like "2017-11-14" we need date format string "Y-m-d". I am able to produce it using php date function and class. It works as expected.

/////Using date function////
echo date("Y-m-d");

/////Using DateTime class////
$date = new DateTime();
echo $date->format('Y-m-d');

Now I want to use these date formats dynamically in my Symfony3 application. As per docs, format for dateType field must be according to "RFC3339" because it uses IntlDateFormatter class for formatting. https://symfony.com/doc/current/reference/forms/types/date.html#format

So to produce date same as previous one, we need different format string

$date = new DateTime();
$dateFormat = new IntlDateFormatter(
    "en_US",
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd'
);
echo $dateFormat->format($date->getTimestamp());

To render datepicker fields on my forms, I am using bootstrap datepicker (https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#format). Format demand to produce same previous date here is bit different "yyyy-mm-dd".

Although I can understand datepicker format can be different from PHP one. But I am unable to understand why there is difference between PHP and symfony date format requirements.

I am not sure how to make it working, as there can be plenty of formats, user can choose from. So a user chooses his date format preferences and I need to produce same date string everywhere.

I am thinking about some converter which can transform PHP date string to symfony and javascript formats or if I can manage to change dateformatter for symfony it can help also in my opinion.

hanish singla
  • 782
  • 8
  • 21

1 Answers1

0

You are confusing the symfony format and html5 one. There is no symfony format, the rfc refers to the html specifications.

You should, for your frontend:

  1. Create a twig filter that output any datetime in the user format preference
  2. Use a javascript date widget instead of html date input. Mainly because the format of html date widget can't be changed and you won't be able to use the user format preference of your application. It is the format that is given to the request that is at the RFC3339 format: YYYY-mm-dd

That's all, your controllers and model should just handle Datetimes object

goto
  • 7,908
  • 10
  • 48
  • 58
  • this can'y be true because I have disabled HTML5 for this field. Here are options I have used: `'widget' => 'single_text', // do not render as type="date", to avoid HTML5 date pickers 'html5' => false, 'format' => 'yyyy-MM-dd', 'jsformat' => 'yyyy-mm-dd',` jsformat is used by datepicker – hanish singla Nov 14 '17 at 11:35
  • By a html datepicker or an external one? – goto Nov 14 '17 at 12:18
  • I am using external bootstrap datepicker. https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#format – hanish singla Nov 14 '17 at 12:54
  • Its very strange that symfony is using a formatter different than default PHP. There is not even any function to convert to each other. – hanish singla Nov 14 '17 at 12:57
  • I don't understand what is troubling you that much about format. The yyyy-MM-dd is a html format because the input generated by the form is html – goto Nov 14 '17 at 13:02
  • Only trouble is that "format" option is not consistent. when I use one value for it, everything goes good. But when I try to pass it dynamically, it messes up because I have to convert php date format to symfony format here. e.g. you can try to pass `'format' => 'Y-m-d'` – hanish singla Nov 14 '17 at 13:57
  • Oh sorry i didn't get that. I guess you should use a textType with a data transformer. – goto Nov 14 '17 at 14:21
  • Yes . . It may be a choice but I don't want to re-invent the wheel for it. :( – hanish singla Nov 15 '17 at 04:47