Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG_add_formatted_short_month.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Added
- `DateUtils::getFormattedShortMonth` + tests Return short month locale format
25 changes: 25 additions & 0 deletions src/Utils/DateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,31 @@ public static function getFormattedLongMonth(\DateTime $date, string $locale = '
return ucfirst((string) $dateFormatter->format($date));
}

/**
* Return short month locale format, substring 3 char except "Juin"
*
* <pre>
* <?php
* getFormattedShortMonth(new \DateTime('2022-01-15'));
* ?>
* </pre>
* The above example will output:
* <pre>
* Jan
* </pre>
*/
public static function getFormattedShortMonth(\DateTime $date, string $locale = 'fr_FR'): string
{
$month = self::getFormattedLongMonth($date, $locale);

// It must be "Juin" in full otherwise there be multiple "Jui." key with "Juillet"
if ($month === 'Juin') {
return $month;
}

return mb_substr((string)$month, 0, 3);
}

/**
* Return full month year locale format
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Utils/DateUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,23 @@ public function getGetFormattedLongMonthProvider(): array
];
}

/**
* @dataProvider getGetFormattedShortMonthProvider
*/
public function testGetFormattedShortMonth(string $expected, string $date, string $locale): void
{
$this->assertEquals($expected, DateUtils::getFormattedShortMonth(new \DateTime($date), $locale));
}

public function getGetFormattedShortMonthProvider(): array
{
return [
'Jan (fr)' => ['Jan', '2022-01-15', 'fr_FR'],
'Juin' => ['Juin', '2020-06-01', 'fr_FR'],
'Jan (en)' => ['Jan', '2020-01-01', 'en_EN'],
];
}

/**
* @dataProvider getGetFormattedLongMonthYearsProvider
*/
Expand Down