1

most likely a silly question, but I do not see how to do.

Within a controller, I need to use $myObj->getData()->format('d-M-Y'), and I wish to get a localized string too (in italian instead than in english).

Within a twig template, I get it by {{ myobj.data|localizeddate('long', 'none', app.request.locale ) }}, but I do not know a similar trick for the former case.


Yes, I found how to do (thanks to ccKep):

$cal = IntlCalendar::fromDateTime($oldObj->getData()->format('d-M-Y')." Europe/Rome");

$newObj->setField("my date is ". IntlDateFormatter::formatObject($cal, "d MMMM YYYY", 'it_IT')."");

1
  • Have a look at datefmt_format. Commented Oct 9, 2017 at 22:18

1 Answer 1

3

In controller you may use Intl. For example:

$intl = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, null, 'd-LLL-y');

$date = $intl->format(new \DateTime('now'));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I am not sure how to use your code in my case; I already have a object, I need to get its date, and then to format according to a local.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.