Time difference is not showing properly - php

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.

Related

PHP Display Days, hours and minutes

i am trying to display 'Days' and 'hours' code is working fine, but issue is i don't want to show 0 days. if day= 0 then show only hours.
<?php
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%a days %h hours');
echo $elapsed;
?>
Output : - 0 Days 5 Hour
Expected Output: 5 Hour
You can just check whether the number of days is 0 or not, and adjust your output format accordingly. The DateInterval object provides the "d" property which lets you see the number of days (see documentation).
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$format = "%h hours";
if($interval->d > 0) $format = "%a days ".$format; //adjust the format according to the number of days
$elapsed = $interval->format($format);
echo $elapsed;
Demo: http://sandbox.onlinephpfunctions.com/code/d01ae70566b1b4466664fcff8f7c70b261766c48
You just have to check if the days are 0:
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format( $interval->format('%a') ? '%a days %h hours' : '%h hours' );
echo $elapsed;

how to calculate the different between two datetime on seconds?

i want to calculate different between two datetime in seconds and check if the result > 300sec
$d1 = new DateTime("2016-03-25 19:29:21");
$d2 = new DateTime(date('Y-m-d H:i:s'));
please check following code:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2016-03-25 19:29:21');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;
check this
if($d2->format('U')-$d1->format('U')>300){
// greater than 300
}else{
// less than 300
}

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

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