CodeIgniter 4 Getter And Setter Time Class Methods

Reading Time: 5 minutes
3,647 Views

Already we have an article over Time class methods in CodeIgniter 4, Click here to see. Time class provides many methods which is very useful when we work with date and time values.

Additionally, it provides some pair of getter and setter functions. Inside this article we will understand the concept of CodeIgniter 4 Getter and Setter time class methods.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


What are Getters And Setters in Time Class?

Time class methods like now(), today(), parse() etc provides the instance of Time class. It display the complete value for date and time.

But in case just think about if we want each individual value like day, month, year, hour, minute, second etc. So here comes the concept of Getter methods of Time class.

Before using Time class, don’t forget to include use CodeIgniter\I18n\Time;

By the help of Getter methods we can get each individual value.

Example

echo $time->getYear();

echo $time->getMonth()

If we want to create a custom date and time value or say we want to set some different date and time value into time class instance. By the value of setter methods we can easily set our custom values to instance of time class.

Example

$time = $time->setYear(2017); 

$time = $time->setMonthNumber(4); 

All of the following getX and setX methods can also be used as if they were a class property. So, any calls to methods like getYear can also be accessed through $time->year.


Getter Methods of Time Class

Here, we will see the available methods in time class which helps to get individual values from a time class instance.

Example

$time = Time::parse('August 12, 2016 4:15:23pm');

The following basic getters exist.

# Using as a property

echo $time->getYear();      // 2016 

echo $time->getMonth();     // 8 

echo $time->getDay();       // 12 

echo $time->getHour();      // 16 

echo $time->getMinute();    // 15 

echo $time->getSecond();    // 23 

# Using as a property

echo $time->year;           // 2016 

echo $time->month;          // 8 

echo $time->day;            // 12 

echo $time->hour;           // 16 

echo $time->minute;         // 15 

echo $time->second;         // 23

In addition to these, a number of methods exist to provide additional information about the date.

$time = Time::parse('August 12, 2016 4:15:23pm'); 

echo $time->getDayOfWeek();     // 6 - but may vary based on locale's starting day of the week 

echo $time->getDayOfYear();     // 225 

echo $time->getWeekOfMonth();   // 2 

echo $time->getWeekOfYear();    // 33 

echo $time->getTimestamp();     // 1471018523 - UNIX timestamp 

echo $time->getQuarter();       // 3 

echo $time->dayOfWeek;          // 6 

echo $time->dayOfYear;          // 225 

echo $time->weekOfMonth;        // 2 

echo $time->weekOfYear;         // 33 

echo $time->timestamp;          // 1471018523 

echo $time->quarter;            // 3

Setter Methods of Time Class

Here we have few methods which helps to create a custom date time value or to update time class instance value.

The following basic setters exist. If any of the values set are out of range, an  InvalidArgumentExeption will be thrown

Note

  • All setters will return a new Time instance, leaving the original instance untouched.
  • All setters will throw an InvalidArgumentException if the value is out of range.

Example

$time = $time->setYear(2017); 

$time = $time->setMonthNumber(4);           // April 

$time = $time->setMonthLongName('April'); 

$time = $time->setMonthShortName('Feb');    // February 

$time = $time->setDay(25); 

$time = $time->setHour(14);                 // 2:00 pm 

$time = $time->setMinute(30); 

$time = $time->setSecond(54);

We hope this article helped you to learn CodeIgniter 4 Getter And Setter Time Class Methods in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more