Table of Contents
If we have two or more than two dates and times. In case if we want some differences – After, Before, same, equal how can we see. In codeigniter 4 we have few functions of Time class, by the help of which we can easily do these things.
In this article we will discuss the concept of compare dates and times in codeigniter 4 tutorial.
In addition we have few more articles, please check these also –
- Find Date Differences in CodeIgniter 4 Tutorial
- CodeIgniter 4 Getter And Setter Time Class Methods
- Concept of Date And Time in CodeIgniter 4 Tutorial
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.
Settings Environment Variables
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 is Time Class & Create Instance
This class is the Time class and lives in the CodeIgniter\I18n namespace. Inside this article we will see the concept of Date and Time in CodeIgniter 4. Also we will see available methods of Time Class in great detail.
To use Time Class in CodeIgniter 4 application, we need to do these things –
- Import CodeIgniter\I18n\Time
- Create Instance of Time()
Here, we have few examples to load and use Time() class.
use CodeIgniter\I18n\Time; $myTime = new Time('+3 week');
Compare Functions in CodeIgniter 4
Here, we have few functions of Time class which we can use to compare date and time instance values.
It determines if the datetime passed in is equal to the current instance. Equal in this case means that they represent the same moment in time, and are not required to be in the same timezone, as both times are converted to UTC and compared.
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago'); $time2 = Time::parse('January 11, 2017 03:50:00', 'Europe/London'); $time1->equals($time2); // true $time1->equals('January 11, 2017 03:50:00', 'Europe/London'); // true
This is identical to the equals method, except that it only returns true when the date, time, AND timezone are all identical.
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago'); $time2 = Time::parse('January 11, 2017 03:50:00', 'Europe/London'); $time1->sameAs($time2); // false $time2->sameAs('January 10, 2017 21:50:00', 'America/Chicago'); // true
Checks if the passed in time is before the the current instance. The comparison is done against the UTC versions of both times.
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago'); $time2 = Time::parse('January 11, 2017 03:50:00', 'America/Chicago'); $time1->isBefore($time2); // true $time2->isBefore($time1); // false
The value being tested against can be a Time instance, a DateTime instance, or a string with the full date time in a manner that a new DateTime instance can understand.
$time1->isBefore('March 15, 2013', 'America/Chicago'); // false
Works exactly the same as isBefore() except checks if the time is after the time passed in.
$time1 = Time::parse('January 10, 2017 21:50:00', 'America/Chicago'); $time2 = Time::parse('January 11, 2017 03:50:00', 'America/Chicago'); $time1->isAfter($time2); // false $time2->isAfter($time1); // true
We hope this article helped you to Compare Dates and Times in CodeIgniter 4 Tutorial 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.