Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter asDate() return wrong number #16539

Closed
sobhan93 opened this issue Jul 19, 2018 · 15 comments
Closed

Formatter asDate() return wrong number #16539

sobhan93 opened this issue Jul 19, 2018 · 15 comments
Assignees

Comments

@sobhan93
Copy link

\Yii::$app->formatter->asDate(time(), 'php:t') return current date.
I think formatter should return number of days in the current month.

echo \Yii::$app->formatter->asDate(time(), 'php:t')

What is the expected result?

31

What do you get instead?

20180719 09:49 PM

Additional info

Q A
Yii version 2.0.15.1
PHP version 7.2.6
Operating system Windows 7 + XAMPP
@rob006 rob006 added the status:to be verified Needs to be reproduced and validated. label Jul 22, 2018
@samdark
Copy link
Member

samdark commented Jul 27, 2018

t in PHP indeed stands for "Number of days in the given month".

@bizley
Copy link
Member

bizley commented Jul 27, 2018

Not supported by ICU and already mentioned in code.

I guess this should work as equivalent

Yii::$app->formatter->asDate('last day of this month', 'd');

@rob006
Copy link
Contributor

rob006 commented Jul 27, 2018

What is the reason of converting PHP date format to ICU?

@rob006 rob006 added type:bug Bug and removed status:to be verified Needs to be reproduced and validated. labels Jul 27, 2018
@bizley
Copy link
Member

bizley commented Jul 27, 2018

To use features of IntlDateFormatter when intl is loaded. PHP date format works here as a fallback if intl is not available.

@rob006
Copy link
Contributor

rob006 commented Jul 27, 2018

What features?

@bizley
Copy link
Member

bizley commented Jul 27, 2018

Here is implementation. Locale based date parsing.

@rob006
Copy link
Contributor

rob006 commented Jul 27, 2018

Any example? I still can't imagine situation where I want to specify format using php: prefix and use IntlDateFormatter features.

@bizley
Copy link
Member

bizley commented Jul 27, 2018

Well, I agree with you.
@samdark was it made like this for consistency?

@samdark
Copy link
Member

samdark commented Jul 28, 2018

@cebe implemented it...

@cebe
Copy link
Member

cebe commented Jul 28, 2018

you can use the PHP format if you want, but you can not use every PHP formatting feature. Intl formatter is better in formatting dates locale-aware. The format conversion is used if you use jquery data picker and similar stuff on the frontend, some use php format, jquery even has its own format, so we allow specifying the format in different ways so that backend and frontend as well as validator use the same specification.

In the case of this issue there is no point in using the Yii formatter, if you only need the number of days in the current month, you can just use date('t'). The formatter is meant to be used to format dates for display to users. t does not fit into this use case as far as I see.

@cebe cebe closed this as completed Jul 28, 2018
@rob006
Copy link
Contributor

rob006 commented Jul 28, 2018

@cebe It still does not explain what is the point of converting PHP format to ICU. If someone wants to use IntlDateFormatter features, he will not use php: prefix.
And t is only one of many unsupported formats that does not work because of current implementation.

@rob006 rob006 reopened this Jul 28, 2018
@sobhan93
Copy link
Author

sobhan93 commented Jul 28, 2018

I need the number of days in the persian calendar.

'formatter' => [
    'locale' => 'fa_IR@calendar=persian',
    'calendar' => \IntlDateFormatter::TRADITIONAL,
],
\Yii::$app->formatter->asDate(time(), 'php:t')

@sobhan93
Copy link
Author

sobhan93 commented Aug 1, 2018

@cebe
@samdark

@cebe cebe added this to the 2.0.16 milestone Sep 27, 2018
@cebe
Copy link
Member

cebe commented Nov 27, 2018

I need the number of days in the persian calendar.

it's simply not supported by intl. Not sure there is a way to add it. If anyone has ideas about how we can add it feel free to propose ideas.

@samdark
Copy link
Member

samdark commented Jun 18, 2019

Won't be implemented in 2.0.

@samdark samdark closed this as completed Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants