How to Get Time Difference Between Two DateTimes in Minutes using PHP
How to Get Time Difference Between Two DateTimes in Minutes using PHP
There are two ways to get the time difference in minutes with PHP.
1. Use the PHP DateTime class to calculate the difference between two date-times. The diff() method of DateTime class creates a DateInterval object that calculates the difference between two date/time objects in time (total days, years, months, days, hours, minutes, seconds, etc.).
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$start_datetime = new DateTime($datetime_1);
$diff = $start_datetime->diff(new DateTime($datetime_2));
echo $diff->days.' Days total<br>';
echo $diff->y.' Years<br>';
echo $diff->m.' Months<br>';
echo $diff->d.' Days<br>';
echo $diff->h.' Hours<br>';
echo $diff->i.' Minutes<br>';
echo $diff->s.' Seconds<br>';
Calculate and get the time difference in minutes:
$total_minutes = ($diff->days * 24 * 60);
$total_minutes += ($diff->h * 60);
$total_minutes += $diff->i;
echo 'Diff in Minutes: '.$total_minutes;
2. You can use strtotime() function to get the time difference between two dates (DateTime) in minutes using PHP.
<?php
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$from_time = strtotime($datetime_1);
$to_time = strtotime($datetime_2);
$diff_minutes = round(abs($from_time - $to_time) / 60,2). " minutes";
Related Posts:
- How To Setup LVS (Linux Virtual Server) Load Balancer on Rocky Linux 8.5
- How To Install Magento 2.4 on Rocky Linux 8
- How To Install Docker on Oracle Linux Server 9
- How to Install WHM/cPanel in Almalinux?
- How To Install Ruby on Rails with PostgreSQL on Rocky Linux 8
- How To Install EMQX MQTT Broker on Rocky Linux 8
- How To Install Kamailio SIP Server on Rocky Linux 8
- How to Install and Configure RabbitMQ Server on Rocky Linux 8
- Setup Quick DNS Server On Rocky Linux 8.5 Using Dnsmasq
- 2 Ways To install Ruby On Rocky Linux | RVM
Latest Posts
- Server-Side Scripting: PHP, Node.js, Python – A Detailed Comparison
- Securing Your Website in 2024: Essential Strategies for Online Safety
- The Future of Web Development Technologies: Trends to Watch in 2024
- How Banks Handle Server-Side Operations and Ensure System Security: An Inside Look
- Tips for Writing Clean, Understandable, and Efficient Code: Avoiding Garbage Code
- Tailwind CSS: Revolutionizing Modern Web Design
- Basic Linux Commands for Beginners: A Starter Guide
- Dairy Farming Loan Apply
- BSNL Recharge Plan
- Bijli Bill Mafi Yojana Online Apply
Technical
- DevOps Roadmap
- How To Install and Configure an SNMP on Ubuntu 20.04
- Apple releases iOS 18 Developer Beta 2 with iPhone screen mirroring, RCS toggle,and more
- How to enable SNMP on Ubuntu Linux 18.04 and above
- How to Force HTTPS Using .htaccess (Updated 2024)
- Display All PHP Errors: Basic & Advanced Usage
- PHP alert
- MongoDB loads but breaks, returning status=14
- MongoDB database deleted automatically
- MongoDB all Error Solutions
Category