Welcome Guest, Not a member yet? Register   Sign In
incompatible with DateTimeImmutable
#1

Hi,

i trie to setup/install codeigniter4, for the moment i recieved this error.

"Fatal error: Declaration of CodeIgniter\I18n\TimeTrait::createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null) must be compatible with DateTimeImmutable::createFromTimestamp(int|float $timestamp): CodeIgniter\Autoloader\Autoloader in /var/www/vhosts/hosting116540.a2f7e.netcup.net/v2.4u.tools/httpdocs/system/I18n/TimeTrait.php on line 268"

What i have to do?
Reply
#2

Dont use php8.4
 Framework supported php8.3
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 12-06-2024, 11:36 PM by X3n-0N.)

(11-20-2024, 01:01 PM)4usol Wrote: Hi,

i trie to setup/install codeigniter4, for the moment i recieved this error.

"Fatal error: Declaration of CodeIgniter\I18n\TimeTrait::createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null) must be compatible with DateTimeImmutable::createFromTimestamp(int|float $timestamp): CodeIgniter\Autoloader\Autoloader in /var/www/vhosts/hosting116540.a2f7e.netcup.net/v2.4u.tools/httpdocs/system/I18n/TimeTrait.php on line 268"

What i have to do?



Update this method as follows : 

Code:
public static function createFromTimestamp(int|float $timestamp, $timezone = null, ?string $locale = null)
{
    // If a timezone is not passed, use the default timezone
    if ($timezone === null) {
        $timezone = new \DateTimeZone(date_default_timezone_get());
    }

    // Create a DateTimeImmutable object from the timestamp
    $dateTime = new \DateTimeImmutable('@' . $timestamp, $timezone);

    // Return a new instance of your class, assuming it's using TimeTrait
    return new self($dateTime->format('Y-m-d H:i:s'), $timezone, $locale);
}
Reply
#4

i dont know ,i had some issues with time so did this :
in BaseController.php
Code:
namespace App\Controllers;
use CodeIgniter\I18n\Time;

protected $theDate;
     protected $theTime;
    
    
     public function __construct()
     {
        
        helper (['text','date','uri','html','form','secuity','numner']);
        $this->theTime =now('Europe/London');
        
    }
    
     protected function getTime()
     {
         return $this->theTime;
        
     }

then in any controller :

Code:
use CodeIgniter\I18n\Time;
public function __construct()
                    {
                        parent::__construct();
                        $this->myTime = parent::getTime();
                        $this->myDate= date("d/m/Y",$this->myTime);     

                    }

    public function doPaginate()
                    {
                    //check to see if any blogs
                     $this->handle = new BlogModel();    
                    $this->isEmptyBlog= $this->handle->count();
                        if( $this->isEmptyBlog == 0)
                    
                                    {
                                    $data =[
                                    'title'=>'checking if any blogs',
                                    'info'=>'sorry there are currently no blogs' ,
                                    'date'=>$this->myDate
                                                        ];
                                    echo view('info',$data);    
                                        
                                    }
                                      
                      elseif(    $this->isEmptyBlog > 0)
{
...
}
I dont want a situation of could be this ,could be that. I set up to use ,time, i call construct at beginning of each controller, then i could have a hundred methods in each controller, all i need to pass to view is :
Code:
'date'=>$this->myDate

and in view


Code:
<?php echo $date;?>


Now since i use a single template for all views , i only need to do above for view once. I acknowledge i have not answered the question but may have thrown in some ideas
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#5

I just installed it for the first time.  I got the same error.

I looked at both solution provided on this thread and noticed that the error specifically says.
PHP Code:
Declaration of CodeIgniter\I18n\TimeTrait::createFromTimestamp(int $timestamp$timezone null, ?string $locale nullmust be compatible with DateTimeImmutable::createFromTimestamp(int|float $timestamp): 

So I went to
vendor/codeigniter4/framework/system/I18n/TimeTrait.php
And changed the declaration on line 268 from
PHP Code:
public static function createFromTimestamp(int $timestamp$timezone null, ?string $locale null
To
PHP Code:
public static function createFromTimestamp(int|float $timestamp$timezone null, ?string $locale null

I am not getting the error anymore.  May not be a complete fix?  But at least the home page of a fresh install loads now.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB