Compare Dates and Times in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes

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 –

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 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.

  • equals()
  • sameAs()
  • isBefore()
  • isAfter()


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.

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.

Find More on CodeIgniter 4 here