Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Time::humanize() does not work with ar locale
I set arabic language in app config file for defualtLocale like this:

`public $defaultLocale = 'ar';`

Now, I work with `humanize` time function like this:

```
$time = CodeIgniter\I18n\Time::parse($date, 'America/Chicago');
return $time->humanize(); 
```

Now in action I get this error:

```

DateTime::__construct(): Failed to parse time string (٢٠٢١-٠٥-١٩ ٠١:٤٧:٢٨) at position 0 (�): Unexpected character

SYSTEMPATH\I18n\Time.php at line 1150

public function humanize()
1149     {
1150         $now  = IntlCalendar::fromDateTime(Time::now($this->timezone)->toDateTimeString());

```

I think the `DateTime` function have problem with arabic number. because when I set `en` for default locale, `humanize function` work truly. 

@lonnieezell @MGatner 


Discussion:
No need to ping, I read every issue and respond according to my ability and time. 

I don't have much experience with Unicode string handling. Hopefully someone familiar will step in. 
I cannot reproduce that this works using the default locale `en`. Even this simple script fails.
```php
$date = '٠١:٤٧:٢٨';
$time = (new DateTime())->modify($date);
var_dump($time);
```

Unfortunately, this is not a CI4 bug. This is an inherent limitation of the `DateTime` parser used by PHP. It can only understand Latin input as described [here](https://www.php.net/manual/en/datetime.formats.compound.php).
@paulbalandan Sure, You right. But if we need to a multilingual system, humanize not work truly(for `en` it's ok, for others is not ok). I think CI4 doesn't need to convert/translate numbers.
Yes, I understand what you want. But this issue is not limited to `Time`'s humanize() function. The issue lies internally in the PHP core. Since PHP's own `DateTime` doesn't understand Arabic dates, naturally it follows that CI4's `Time` (which is an extension of `DateTime`) will also fail with Arabic dates. As you can see, even `DateTime::modify()` fails parsing the date so this is outside CI4.

What I can see as a potential solution is to have first a converter to/from Arabic dates. Something like `strtr` replacements. After conversion, pass it to DateTime/Time. However, I don't see that converter fit into the framework as we would need to also cater other languages with non-Latin datetimes.
I've confirmed this bug.

```diff
diff --git a/app/Config/App.php b/app/Config/App.php
index c6c716822..b697a23c5 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -70,7 +70,7 @@ class App extends BaseConfig
      *
      * @var string
      */
-    public $defaultLocale = 'en';
+    public $defaultLocale = 'ar';
 
     /**
      * --------------------------------------------------------------------------
@@ -97,7 +97,7 @@ class App extends BaseConfig
      *
      * @var string[]
      */
-    public $supportedLocales = ['en'];
+    public $supportedLocales = ['ar', 'en'];
 
     /**
      * --------------------------------------------------------------------------
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 7f867e95f..ff48fb5f2 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,8 @@ class Home extends BaseController
 {
     public function index()
     {
-        return view('welcome_message');
+        $date = '2022-06-06';
+        $time = \CodeIgniter\I18n\Time::parse($date, 'America/Chicago');
+        return $time->humanize();
     }
 }
```
```
$ php public/index.php 


[Exception]

DateTime::__construct(): Failed to parse time string (٢٠٢٢-٠٦-١٤ ٠٠:٢٨:١٠) at position 0 (?): Unexpected character

at SYSTEMPATH/I18n/Time.php:1008

Backtrace:
  1    [internal function]
       DateTime()->__construct('٢٠٢٢-٠٦-١٤ ٠٠:٢٨:١٠')

  2    SYSTEMPATH/I18n/Time.php:1008
       IntlCalendar::fromDateTime('٢٠٢٢-٠٦-١٤ ٠٠:٢٨:١٠')

  3    APPPATH/Controllers/Home.php:11
       CodeIgniter\I18n\Time()->humanize()

  4    SYSTEMPATH/CodeIgniter.php:898
       App\Controllers\Home()->index()

  5    SYSTEMPATH/CodeIgniter.php:468
       CodeIgniter\CodeIgniter()->runController(Object(App\Controllers\Home))

  6    SYSTEMPATH/CodeIgniter.php:351
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

  7    FCPATH/index.php:55
       CodeIgniter\CodeIgniter()->run()
```


@perspolise 
I've fixed this bug. See #6120

But the output is like this:
```
١ week ago
```
Is this correct?

(Added)
https://www.saitak.com/Number/
`1` is `۱`. Okay.





Patch:
diff --git a/system/I18n/Time.php b/system/I18n/Time.php
index 64b4d350e8fd..659a0a252db2 100644
--- a/system/I18n/Time.php
+++ b/system/I18n/Time.php
@@ -82,7 +82,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
         // If a test instance has been provided, use it instead.
         if ($time === '' && static::$testNow instanceof self) {
             $timezone = $timezone ?: static::$testNow->getTimezone();
-            $time     = (string) static::$testNow->toDateTimeString();
+            $time     = static::$testNow->format('Y-m-d H:i:s');
         }
 
         $timezone       = $timezone ?: date_default_timezone_get();
@@ -1005,7 +1005,7 @@ public function isAfter($testTime, ?string $timezone = null): bool
      */
     public function humanize()
     {
-        $now  = IntlCalendar::fromDateTime(self::now($this->timezone)->toDateTimeString());
+        $now  = IntlCalendar::fromDateTime(self::now($this->timezone));
         $time = $this->getCalendar()->getTime();
 
         $years   = $now->fieldDifference($time, IntlCalendar::FIELD_YEAR);
@@ -1106,7 +1106,7 @@ public function getUTCObject($time, ?string $timezone = null)
      */
     public function getCalendar()
     {
-        return IntlCalendar::fromDateTime($this->toDateTimeString());
+        return IntlCalendar::fromDateTime($this);
     }
 
     /**


Test patch:
diff --git a/tests/system/I18n/TimeTest.php b/tests/system/I18n/TimeTest.php
index 03f142479478..2f2980f2411e 100644
--- a/tests/system/I18n/TimeTest.php
+++ b/tests/system/I18n/TimeTest.php
@@ -11,8 +11,10 @@
 
 namespace CodeIgniter\I18n;
 
+use CodeIgniter\Config\Factories;
 use CodeIgniter\I18n\Exceptions\I18nException;
 use CodeIgniter\Test\CIUnitTestCase;
+use Config\App;
 use DateTime;
 use DateTimeZone;
 use IntlDateFormatter;
@@ -1025,6 +1027,31 @@ public function testHumanizeNow()
         $this->assertSame('Just now', $time->humanize());
     }
 
+    /**
+     * @see https://github.com/codeigniter4/CodeIgniter4/issues/4708
+     */
+    public function testHumanizeWithArLocale()
+    {
+        $this->resetServices();
+
+        $currentLocale = Locale::getDefault();
+        Locale::setDefault('ar');
+
+        $config                   = new App();
+        $config->supportedLocales = ['ar'];
+        $config->defaultLocale    = 'ar';
+        Factories::injectMock('config', 'App', $config);
+
+        Time::setTestNow('2022-06-14 12:00', 'America/Chicago');
+
+        $date = '2022-06-07 12:00';
+        $time = Time::parse($date, 'America/Chicago');
+
+        $this->assertSame('١ week ago', $time->humanize());
+
+        Locale::setDefault($currentLocale);
+    }
+
     public function testSetTimezoneDate()
     {
         $time  = Time::parse('13 May 2020 10:00', 'GMT');
@@ -1058,4 +1085,18 @@ public function testUnserializeTimeObject()
         $this->assertTrue($time2->equals($time1));
         $this->assertNotSame($time1, $time2);
     }
+
+    public function testSetTestNowWithFaLocale()
+    {
+        $currentLocale = Locale::getDefault();
+        Locale::setDefault('fa');
+
+        Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');
+
+        $now = Time::now()->format('c');
+
+        $this->assertSame('2017-03-10T12:00:00+09:00', $now);
+
+        Locale::setDefault($currentLocale);
+    }
 }