Skip to content

strtotime returns false when string includes narrow non breaking space character #11600

Closed

Description

@myleshyson

Description

This is kind of related to #9799

With the ICU 72.1 update, it replaces a ascii space character with a narrow non-breaking space unicode character for intl space So something like

(new IntlDateFormatter('en_US', -1, 3))->getPattern();

will return h:mm\u202fa instead of h:mm a.

When creating date strings based off that pattern with the NNBS character, and then passing into strtotime, strtotime returns false.

With the ICU update, I think functions like strtotime should support parsing the NNBS character.

The following code:

<?php
$pattern = (new IntlDateFormatter('en_US', -1, 3))->getPattern();
$timeString = date($pattern, date('now'));
$timestamp = strtotime($timeString);

Resulted in this output:

false

But I expected this output instead:

a unix timestamp

PHP Version

8.2.8

Operating System

Redhat Enterprise 8.8

Activity

  1. ndossche commented on Jul 6, 2023

    @ndossche
    Member

    Looks like it calls into timelib_strtotime() so this needs changes in timelib, if this change is wanted.

  2. self-assigned this
    on Jul 6, 2023
  3. hormus commented on Jul 10, 2023

    @hormus

    For date or DateTime https://www.php.net/manual/en/datetime.formats.time.php
    fornat ICU h:mm a or h:mm\u202fa missing for date or DateTime.
    change date('now') to strtotime('now')

  4. added a commit that references this issue on Jul 11, 2023
  5. derickr commented on Jul 11, 2023

    @derickr
    Member

    Fixed for PHP 8.2.9 — thanks for your report, but please note that your example is not correct. The pattern that comes back from getPattern() is h:mm&nnbsp;a — which when formatting does the m (the minutes) twice, resulting in something like 04:3232 PM that can not be parsed.

    Instead, I had to modify the test to: https://github.com/php/php-src/blob/master/ext/date/tests/bug-gh11600.phpt

  6. myleshyson commented on Jul 11, 2023

    @myleshyson
    Author

    Sweet thanks! Got so focused onthe \u202f character breaking strtotime I missed the extra m breaking it as well. Either way happy it's fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions