Search code examples
phpcodeignitercodeigniter-4

How do I use setMonth() method in Codeigniter 4


I am currently working on getting the month of the date using the Setter function in Codeigniter 4. I can't figure out how the $time variable obtained since there is no where it is been defined.

echo $time = $time->setMonth(4);

Am getting an undefined variable error. Please help


Solution

  • You have to first instantiate time, and then you can use the setters as in the documentation. For example;

    <?php
    
    namespace App\Controllers;
    
    Use CodeIgniter\I18n\Time;
    
    class Mok extends BaseController
    {
    
        public function time() {
    
            $time = Time::now('America/Chicago', 'en_US');
            $time = $time->setMonth(4);       // April
            echo $time;
        }
    }