2

Example code:

$a = new IntlDateFormatter('en-US', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/London', IntlDateFormatter::GREGORIAN);
var_dump($a->format(strtotime('2021-09-17 15:00')));

$a = new IntlDateFormatter('sv-SE', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/London', IntlDateFormatter::GREGORIAN);
var_dump($a->format(strtotime('2021-09-17 15:00')));

Actual output:

string(26) "Friday, September 17, 2021"
string(24) "fredag 17 september 2021"

Wanted output:

string(26) "Friday, September 17"
string(24) "fredag 17 september"

Changing IntlDateFormatter::FULL to IntlDateFormatter::LONG does not help. It only makes the output like this:

string(18) "September 17, 2021"
string(17) "17 september 2021"

Neither of IntlDateFormatter::NONE, IntlDateFormatter::SHORT and IntlDateFormatter::MEDIUM cause the desired output.

I specifically want the "full" one, but just not the year part.

Please do not suggest any solution which attempts to "hack" away the year, such as string/regexp-removing it. I need the solution to cause the engine to not include the year part in the output. I am sure that other locales have much more complicated/different year parts than the two ones I have used as examples.

user16508174
  • 281
  • 1
  • 6
  • I know you said you didn't want to hack, but I think you might have to a bit. You can get the string used by the formatter by using `$a->getPattern()` which will give you something like `EEEE d MMMM y`. You can then take that pattern, which will use the universal date/time tokens and remove the `y` or `yyyy`, and then call `$a->setPattern()` with your new string. I think this is as close as you are going to get – Chris Haas Sep 17 '21 at 20:30
  • @ChrisHaas Yes, but that's the same basic thing as parsing the final output, only with a "Y" instead of the number. It will still have a random "," here and there, etc. – user16508174 Sep 17 '21 at 20:43
  • Although [this](https://stackoverflow.com/a/35149860/231316) is from 5 years ago, I don't think any additional work has been done to add the feature you are looking for. – Chris Haas Sep 17 '21 at 21:04
  • Actually, looks like this will be landing in 8.1! https://wiki.php.net/rfc/intldatetimepatterngenerator – Chris Haas Sep 17 '21 at 21:06

1 Answers1

2

Each locale will define its own date format, and the contents and ordering will vary. Either you need to define a single pattern for all, allow the user to select their desired pattern from a list, or do the thing that you don't seem to want to, and parse/modify the locale's pattern.

There is not an as-is solution that will simply accomplish your desired output automagically.

https://www.php.net/manual/en/intldateformatter.setpattern.php

https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table

$d = new DateTime('2021-09-17 15:00');

$fmt_en = new IntlDateFormatter('en-US', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
$fmt_en->setPattern('EEEE, MMMM d');

$fmt_sv = new IntlDateFormatter('sv-SV', IntlDateFormatter::NONE, IntlDateFormatter::NONE);
$fmt_sv->setPattern('MMMM d, EEEE');

var_dump(
    $fmt_en->format($d),
    $fmt_sv->format($d)
);

Output:

string(20) "Friday, September 17"
string(20) "september 17, fredag"
Sammitch
  • 30,782
  • 7
  • 50
  • 77