Table of Contents
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.
Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.
Let’s get started.
Download & Install CodeIgniter 4 Setup
We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.
Here are the following ways to download and install CodeIgniter 4 –
- Manual Download
- Composer Installation
- Clone Github repository of CodeIgniter 4
Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.
Here is the command to install via composer –
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
Turn Development Mode On
When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env
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.
CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.
# 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.
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.
$time = $time->setYear(2017); $time = $time->setMonthNumber(4);
All of the following
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
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.
$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
- 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.
$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.
Find More on CodeIgniter 4 here
- CodeIgniter 4 Cookie Helper Tutorial
- CodeIgniter 4 CRUD Application Tutorial
- CodeIgniter 4 CRUD REST APIs Tutorial
- CodeIgniter 4 CSRF Token in AJAX Request
- Database Query in CodeIgniter 4 Tutorial
- CodeIgniter 4 Ajax Form Data Submit
- CodeIgniter 4 Form Validation Tutorial
- CodeIgniter 4 Image Upload with Form Tutorial
- Multi language in CodeIgniter 4 Tutorial
- Stripe Payment Gateway Integration in CodeIgniter 4
- CodeIgniter 4 CSRF Token Tutorial
- CodeIgniter 4 Basics Tutorial
- CodeIgniter 4 Spark CLI Commands Tutorial
- Migration in CodeIgniter 4 Tutorial
- Seeders in CodeIgniter 4 Tutorial
Hi, I am Sanjay the founder of Online Web Tutor. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.