Find Date Differences in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
314 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. You can see the list of those here, Click to learn.

Inside this article we will learn To find date differences in CodeIgniter 4. This article uses the Time class and it’s methods to see the whole concept to find date and time differences in terms of year, month, days, hours, weeks etc.

How can compare two date values, you will get the complete idea from this article.

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'); 

Date Difference Methods in CodeIgniter 4

To compare two Times directly, we use the difference() method, which returns a CodeIgniterI18nTimeDifference instance.

$time = Time::parse('March 10, 2017', 'America/Chicago'); 

$diff = $time->difference(Time::now()); 

$diff = $time->difference(new DateTime('July 4, 1975', 'America/Chicago'); 

$diff = $time->difference('July 4, 1975 13:32:05', 'America/Chicago');

Once you have the TimeDifference instance, we have several methods we can use to find information about the difference between the two times.

$current = Time::parse('March 10, 2017', 'America/Chicago'); 

$test    = Time::parse('March 10, 2010', 'America/Chicago'); 

$diff = $current->difference($test); 

echo $diff->getYears();     // -7 

echo $diff->getMonths();    // -84 

echo $diff->getWeeks();     // -365 

echo $diff->getDays();      // -2557 

echo $diff->getHours();     // -61368

echo $diff->getMinutes();   // -3682080 

echo $diff->getSeconds();   // -220924800

We can use either getX() methods, or access the calculate values as if they were properties:

echo $diff->years;     // -7 

echo $diff->months;    // -84 

echo $diff->weeks;     // -365 

echo $diff->days;      // -2557 

echo $diff->hours;     // -61368 

echo $diff->minutes;   // -3682080 

echo $diff->seconds;   // -220924800

humanize()

This returns a string that displays the difference between the times in a human readable format that is geared towards being easily understood. It can create strings like ‘3 hours ago’, ‘in 1 month’, etc. 

$current = Time::parse('March 10, 2017', 'America/Chicago') 

$test    = Time::parse('March 9, 2016 12:00:00', 'America/Chicago'); 

$diff = $current->difference($test); 

echo $diff->humanize();     // 1 year ago

We hope this article helped you to Find Date Differences 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

Leave a Comment