CodeIgniter Forums
Time::createFromTimestamp always -2 hours in my local time - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Time::createFromTimestamp always -2 hours in my local time (/showthread.php?tid=82370)



Time::createFromTimestamp always -2 hours in my local time - superior - 07-03-2022

Hi,

I'm using the Time class from CodeIgniter 4 to format my datetime, but for some reason the time is always -2 hours.
Checked if function app_timezone() would give correct results, that's without issues 'Europe/Amsterdam' but when using the code below it's always -2 hours.

Did I miss something with settings or am I just using it the wrong way?
PHP Code:
<?php

$created_at 
'2022-07-03 14:38:10';
$timmestamp Time::createFromTimestampstrtotime$created_at ), app_timezone() ); // Correct, Europe/Amsterdam
                        
return $timmestamp->toLocalizedString("d MMMM yyyy 'om' HH:mm");  // Always output with time:  12:38 



RE: Time::createFromTimestamp always -2 hours in my local time - kenjis - 07-03-2022

Cannot reproduce.

I see "3 July 2022 om 14:38".

Code:
diff --git a/app/Config/App.php b/app/Config/App.php
index 1a5e562dd..d1de17941 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -109,7 +109,7 @@ class App extends BaseConfig
      *
      * @var string
      */
-    public $appTimezone = 'America/Chicago';
+    public $appTimezone = 'Europe/Amsterdam';

    /**
      * --------------------------------------------------------------------------
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 7f867e95f..6d98547e8 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -2,10 +2,15 @@

namespace App\Controllers;

+use CodeIgniter\I18n\Time;
+
class Home extends BaseController
{
    public function index()
    {
-        return view('welcome_message');
+        $created_at = '2022-07-03 14:38:10';
+        $timmestamp = Time::createFromTimestamp( strtotime( $created_at ), app_timezone() ); // Correct, Europe/Amsterdam
+
+        return $timmestamp->toLocalizedString("d MMMM yyyy 'om' HH:mm");  // Always output with time:  12:38
    }
}

I got "12:38" on v4.1.9.

Upgrade to latest v4.2.1.