php find total datetime difference in seconds not getting as expected - php

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

Related

PHP add mysql timestamp to existing timestamp

I have mysql timestamps named the following:
$end_time = $row['end_time'];
$paused_time = $row['paused_time'];
$current_time = date("Y-m-d H:i:s");
i need to get the difference between the current time and the paused time, then add that total to the end time.
Essentially what im trying to achieve is when an event is paused, it updates the $paused_time in the database, so when i resume again i need to add the time since it was paused to the end time which will increase it.
say i paused it 1 hour ago, the difference between $paused_time and $current_time is 1 hour, so $end_time will be $end_time + 1 hour which i will then update the database replacing the current end time.
Everything i try is such a mess and not really achieving my goal so i would appreciate how to go about doing this.
$datetime1 = new DateTime($paused_time);
$datetime2 = new DateTime($current_time);
$interval = $datetime1->diff($datetime2);
// add $interval to $end_time??
Many thanks and hope i explained it clear enough
For example:
$end_time = "2019-07-07 14:09:07";
$paused_time = "2019-07-07 13:09:07";
$current_time = date("Y-m-d H:i:s");
$datetime1 = new DateTime($paused_time);
$datetime2 = new DateTime($current_time);
$datetime3 = new DateTime($end_time);
$interval = $datetime1->diff($datetime2);
$sumDateTime = $datetime3->add($interval);
Can you try it with strtotime()? You will get milliseconds out of it and you can convert it back to any unit of time you wish.
$end_time = strtotime($row["end_time"]);
$paused_time = strtotime($row["paused_time"]);
$current_time = time();
$interval = $end_time - $paused_time;
$add_interval_to_end_time = $end_time + $interval;
Cheers
Hanns

Cannot calculate the time difference using 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

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

Time difference is not showing properly

Here is my code : current time - 14.32
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';
The output : 35 Minutes
Why is the minutes is showing 35 instead of 2 minutes?
set the time zone,
date_default_timezone_set("Asia/Kolkata");
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';// for current time, difference is 10 minutes.

php DateTime countdown

So I have a question about a DateTime in php.
$datetime1 = new DateTime('2013-02-01 10:40:00');
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');
What echo outputs is: 2day 8hours 33minutes.
Ok I know that the difference between first and the second variable is equal to the output. But is there any way that the output could be some sort of count down.
For example:
$datetime1 = new DateTime('2013-01-01 00:00:00');
$datetime2 = new DateTime('2013-01-01 13:30:00');
What I want to be output is: 13:30:00, and 2 minutes later there would be 13:28:00.
Is there any way to be done that with diff function.
Thanks for help
Sebastian
This will only work if:
one of the times in now
the page refreshes or you use ajax. PHP is executed on the server-side.
So you basically already have the code with just a tweak:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');

Categories