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. 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.
Learn More –
- User Agent Class Concept in CodeIgniter 4 Tutorial
- Using Basic Auth REST API Development in CodeIgniter 4
- Validate Mobile Number in CodeIgniter 4 Tutorial
- View Cells in CodeIgniter 4 Tutorial with Example
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');
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
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.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.