Compare Dates and Times in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
4,477 Views

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.

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 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()

equals()

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

sameAs()

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

isBefore()

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

isAfter()

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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5