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

PHP-INTL date format #645

Closed
bttrx opened this issue Dec 6, 2023 · 3 comments
Closed

PHP-INTL date format #645

bttrx opened this issue Dec 6, 2023 · 3 comments

Comments

@bttrx
Copy link
Contributor

bttrx commented Dec 6, 2023

Since I setup my HTMLy instance I used to have a custom date format, because I like to also display the time of a post.

Until HTMLy version 2.8.2 I could use a date format of "%A, %e. %B %Y, %H:%M Uhr". Since version 2.8.3 I had to switch to "EEEE, d. F Y, H:i 'Uhr'", because of the use of PHP-INTL.

My issue now is, days 1-9 are shown with a leading zero, because of the mangling in format_date(), i.e., "d" becomes "dd" automatically.

What I would like to see, is either a new custom date format config item, to which no format mangling is applied then. Or the removal of the format mangling at all.

function format_date($date, $dateFormat = null)
{
    if (empty($dateFormat)) {
        $dateFormat = config('date.format');
    }
    if (extension_loaded('intl')) {
        $format_map = array('s' => 'ss', 'i' => 'mm', 'H' => 'HH','d' => 'dd', 'm' => 'MM', 'M' => 'MMM', 'F' => 'MMMM', 'Y' => 'yyyy');
        $intlFormat = strtr($dateFormat, $format_map);
        $formatter = new IntlDateFormatter(config('language'), IntlDateFormatter::NONE, IntlDateFormatter::NONE, config('timezone'), IntlDateFormatter::GREGORIAN, $intlFormat);
        return $formatter->format($date); 
    } else {
        return date($dateFormat, $date);
    }
} 

References:

@danpros
Copy link
Owner

danpros commented Dec 7, 2023

The d in PHP date() displaying date from 01-31, equivalent to dd in ICU and j to d (w/o leading zero). We need to complete the format_map to get full conversion.

In the mean while you can add those manually, add 'j' => 'd' and in config.ini EEEE, j. F Y, H:i

@bttrx
Copy link
Contributor Author

bttrx commented Dec 7, 2023

I see. You mean:

Adding 'j' => 'd' and adjusting config.ini worked. Thanks a lot!

@danpros
Copy link
Owner

danpros commented Dec 11, 2023

Already include it in latest release. I will close this one.

@danpros danpros closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants