From 185cea3d104b24b90e9876060343998aed5f7635 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Mon, 27 Jun 2022 16:51:55 -0500 Subject: [PATCH] Fix Php 8.2 utf8_encode deprecation --- src/Carbon/Traits/Date.php | 8 +++++++- tests/Carbon/StringsTest.php | 2 +- tests/CarbonImmutable/StringsTest.php | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Carbon/Traits/Date.php b/src/Carbon/Traits/Date.php index c3a13a8557..b05b3652c4 100644 --- a/src/Carbon/Traits/Date.php +++ b/src/Carbon/Traits/Date.php @@ -1853,7 +1853,13 @@ public function formatLocalized($format) ? strftime($format, $time) : @strftime($format, $time); - return static::$utf8 ? utf8_encode($formatted) : $formatted; + return static::$utf8 + ? ( + \function_exists('mb_convert_encoding') + ? mb_convert_encoding($formatted, 'UTF-8', mb_list_encodings()) + : utf8_encode($formatted) + ) + : $formatted; } /** diff --git a/tests/Carbon/StringsTest.php b/tests/Carbon/StringsTest.php index 73a49348e0..fda4424ba5 100644 --- a/tests/Carbon/StringsTest.php +++ b/tests/Carbon/StringsTest.php @@ -183,7 +183,7 @@ public function testToLocalizedFormattedDateStringWhenUtf8IsNedded() Carbon::setUtf8(false); $this->assertSame('décembre', $nonUtf8Date); - $this->assertSame(utf8_encode('décembre'), $utf8Date); + $this->assertSame(mb_convert_encoding('décembre', 'UTF-8', mb_list_encodings()), $utf8Date); }); } diff --git a/tests/CarbonImmutable/StringsTest.php b/tests/CarbonImmutable/StringsTest.php index 73dff18468..644dff2fde 100644 --- a/tests/CarbonImmutable/StringsTest.php +++ b/tests/CarbonImmutable/StringsTest.php @@ -116,7 +116,7 @@ public function testToLocalizedFormattedDateStringWhenUtf8IsNedded() Carbon::setUtf8(false); $this->assertSame('décembre', $nonUtf8Date); - $this->assertSame(utf8_encode('décembre'), $utf8Date); + $this->assertSame(mb_convert_encoding('décembre', 'UTF-8', mb_list_encodings()), $utf8Date); }); }