How to get the time difference in minutes in PHP
In this article, we will describe the method of getting the time difference in minutes in PHP.
-
Using
date_diff()
Functions - Using Mathematical Formulas
Use the function in PHP date_diff()
to get the time difference in minutes
We will use the built-in function date_diff()
to get the time difference in minutes.
To do this, we need a start date and an end date. We will use date_diff()
the function to calculate the time difference between them in minutes. The correct syntax for using this function is as follows.
date_diff($DateTimeObject1, $DateTimeObject2);
The built-in function date_diff()
has two parameters. The detailed parameters are as follows
parameter | illustrate | |
---|---|---|
$DateTimeObject1 |
Enforcement |
It is an DateTime object. It represents the start date. |
$DateTimeObject2 |
Enforcement |
It is also an DateTime object, which represents the end date. |
This function returns the difference between the start date and the end date on success, or FALSE on failure.
The following program shows how we can use date_diff()
the function to get the time difference in minutes.
<?php
$dateTimeObject1 = date_create('2019-06-16');
$dateTimeObject2 = date_create('2020-06-16');
$difference = date_diff($dateTimeObject1, $dateTimeObject2);
echo ("The difference in days is:");
echo $difference->format('%R%a days');
echo "\n";
$minutes = $difference->days * 24 * 60;
$minutes += $difference->h * 60;
$minutes += $difference->i;
echo("The difference in minutes is:");
echo $minutes.' minutes';
?>
The function date_diff()
returns an object representing the difference between two dates.
Output:
The difference in days is:+366 days
The difference in minutes is:527040 minutes
Now we will find the time difference.
<?php
$dateTimeObject1 = date_create('17:13:00');
$dateTimeObject2 = date_create('12:13:00');
$difference = date_diff($dateTimeObject1, $dateTimeObject2);
echo ("The difference in hours is:");
echo $difference->h;
echo "\n";
$minutes = $difference->days * 24 * 60;
$minutes += $difference->h * 60;
$minutes += $difference->i;
echo("The difference in minutes is:");
echo $minutes.' minutes';
?>
Output:
The difference in hours is:5
The difference in minutes is:300 minutes
Using math formulas in PHP to get the number of minutes of time difference
In PHP, we can also use different mathematical formulas to get the time difference in minutes. The procedure to get the time difference in minutes is as follows.
<?php
$to_time = strtotime("10:42:00");
$from_time = strtotime("10:21:00");
$minutes = round(abs($to_time - $from_time) / 60,2);
echo("The difference in minutes is: $minutes minutes.");
?>
Output:
The difference in minutes is: 21 minutes
We can also use the following method to calculate the time difference in minutes.
<?php
$start = strtotime('12:01:00');
$end = strtotime('13:16:00');
$minutes = ($end - $start) / 60;
echo "The difference in minutes is $minutes minutes.";
?>
Output:
The difference in minutes is 75 minutes.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of