Cannot calculate the time difference using PHP - php

I need to calculate the datetime difference in minutes using PHP. I am explaining my code below .
$date='10-03-2018 03:44 PM';
$endTime = strtotime($date);
$currentDate=date("d-m-Y h:i A");//10-03-2018 03:53 PM
$currentTime = strtotime($currentDate);
echo (round(abs($currentTime - $endTime) / 60,2));//25344617
Here I need to calculate the difference in minutes but the differnce value is more where the expected time difference should be 9 but as per my code I am getting the wrong value.

Let the PHP DateTime class with diff() method do the work with time calculations.
$now = '10-03-2018 03:53 PM'; // or use simply 'now' for current time
$endTime = '10-03-2018 03:44 PM';
$datetime1 = new DateTime($now);
$datetime2 = new DateTime($endTime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%i minutes'); // 9 minutes
See it live: https://eval.in/969615

Related

How to find the Time Difference between a PM time and AM time?

In my form there are 2 Time Pickers where user can select a from time and to time. It doesn't have a date associated with it. And for a report generating purpose I've to calculate the time difference between them. It works perfectly to if the From and to Time is "06:00 to 10:00" but if the from and to time is "21:00 to 02:00" I get a time difference of 19 hours. Could you please help me to fix this.
For this case "21:00 to 02:00" the time difference should be 5 hours.
This is the Code
$datetime1 = new \DateTime('09:30 PM');
$datetime2 = new \DateTime('02:00 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
exit;
The difference becomes negative If $totime is less than $fromtime.
DateInterval->invert == 1 indicates that. This is used with this short solution to correct the result.
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$diff = date_create($fromtime)->diff(date_create($totime));
$hours = $diff->invert ? 24-$diff->h : $diff->h;
echo $hours; //5
Since you have 2 date pickers one for from time and another to time, the former will always be smaller than the latter. Hence when from time is larger than to time it means user has selected to from the next day. If we don't add a date for calculating difference, PHP will assume today's date by default. We can easily fix this by adding a condition to compare the times and prepend the dates accordingly. Below is the updated code.
<?php
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$now = new \DateTime();
$today = $now->format('Y-m-d'); // Store current date
$now->add(new DateInterval('P1D')); // Add one day to current date to get next date
$nextDay = $now->format('Y-m-d'); // Store next date
if($fromtime > $totime) // If from time is bigger than to time, it means to is a next day
{
$fromdatetime = "$today $fromtime";
$todatetime = "$nextDay $totime";
}
else
{
$fromdatetime = "$today $fromtime";
$todatetime = "$today $totime";
}
$datetime1 = new \DateTime($fromdatetime);
$datetime2 = new \DateTime($todatetime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
?>

Calculate difference between to unix epoch date times?

I need to be able to find out the difference between two unix epoch times.
I am trying this at the moment
$interval = $nextFile-$firstFile;
($nextFile would equal "1452182820", $firstFile would equal "1452004380")
This gets me a result of "178440".
Is taking away two epoch date times away from each other valid? Or should i find the difference another way.
Try This May be help ful
<?php
$nextFile = '1452182820';
$firstFile = '1452004380';
$n = date('d-m-Y H:i:s',$nextFile);
$f = date('d-m-Y H:i:s',$firstFile);
$Date1 = date("Y-m-d", strtotime($n));
$Date2 = date("Y-m-d", strtotime($f));
$datetime1 = new DateTime($Date1);
$datetime2 = new DateTime($Date2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

php find total datetime difference in seconds not getting as expected

I am trying to take the differences of datetimes in php as follows:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2015-09-23 08:09:50');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%S');
echo $elapsed;
Here I am getting the difference in seconds.
I need the total time difference in seconds.
$difference = abs($datetime1->getTimestamp() - $datetime2->getTimestamp());

Calculating time difference for fixed hour

I want to calculate the time difference from now (lets say 18:30:00) till this evening at 20pm.
$today = date('Y-m-d', time());
$remain = strtotime($today. " 00:00:00 + 20 hours") - time();
$remain = date('H:i:s', $remain);
I get a result which is one hour larger (02:30:00) than the actual result (01:30:00). I tried setting time zones but it's always the same result.
Using the DateTime object, you can do this easily:
$d1 = new DateTime('2015-04-23 18:30');
$d2 = new DateTime('2015-04-23 20:00');
$interval = $d2->diff($d1);
echo $interval->format('%H:%i hours');

Find the difference between two time stamps

$diff = strtotime(12:00:00) - strtotime(5:01:29);
echo date('H:i:s', $diff);
result is 12:01:29
Trying to get the result of 5 hrs 1 min 29 sec
I prefer to use the DateTime class for time comparison / addition / subtraction:
$dt1 = new DateTime();
$dt1->setTimestamp(strtotime('12:00:00am'));
$dt2 = new DateTime();
$dt2->setTimestamp(strtotime('5:01:29am'));
$interval = $dt1->diff($dt2);
echo $interval->format('%h hrs %i min %s sec');
Edit: with the format you gave in strtotime it was thinking 5:01am to 12pm - so it was giving a very different answer than what you wanted, so you need to specify am/pm

Categories