how to calculate the different between two datetime on seconds? - php

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
}

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;

time difference Am I doing it right?

I am trying to measure time difference
I am also learning to wright PHP..
I am saving time and date into SQL as datetime by:
$now = new DateTime();
$datenow = $now->format('Y-m-d H:i:s');
$sql = "INSERT INTO Logg ( logdate , Log , value) VALUES ( '$datenow' , 'Log', '$value' )";
I can now do a query and get:
$result = mysqli_query( $conn , $sqlq );
while($row = mysqli_fetch_array($result)) {
$myLogdate = $row['logdate'];
What I am after, is to be able to check the time between
$myLogdate
and
$now = new DateTime();
$datenow = $now->format('Y-m-d H:i:s');
I have tried:
$interval = $datenow->diff($myLogdate);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
But I get no results....
You can use date_diff.
Example from the linked page:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
A list of possible formats can be found here
for finding the date difference try the below code:
$interval = date_diff($datenow, $myLogdate);
echo $interval->format('%R%a days')
and for format the interval (like "%R%a" means +days) follow http://php.net/manual/en/dateinterval.format.php.
The solution provided by waka was:
$then = new DateTime($myLogDate);
$interval = date_diff($now, $then);
echo $interval->format('%R%a days');

days, hours, and minutes remaining in php

I have this timestamp from the database 1496592689
the problem is I don't know how to get the remaining days, hours and minutes
but I have this code bellow
this is my variable from where the timestamp stored $db_user_timestamp
and now I have this current time now $timenow = time();
I tried to calculate it with
$remainingtime = $db_user_timestamp - $timenow;
But I don't know how to put it in the days, hours and minutes.
Thanks in advance for helping me :)
Always use DateTime
$create_time = "1496592689";
$current_time = time();
$dtCurrent = DateTime::createFromFormat('U', $current_time);
$dtCreate = DateTime::createFromFormat('U', $create_time);
$diff = $dtCurrent->diff($dtCreate);
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
echo $interval;
result
6 months 30 days 5 hours 52 minutes
If your PHP version is 5.3 or latest, you should check
http://php.net/manual/en/class.dateinterval.php
and
http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime(date('Y-m-d H:i:s', $db_user_timestamp));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months %d days %h hours %m minutes %s seconds');
<?php
$db_user_timestamp = 1496592689;
$difference = $db_user_timestamp - time();
echo "Day : ".$day = date('d',$difference);
echo "<br>Hour : ".$hour = date('H',$difference);
echo "<br>Minute : ".$minute = date('i',$difference);
?>
Using date('M/d/Y H:i:s', $theTimestamp); will give you date in day, month, year, hour, minute, seconds in the order you want (change 'M/d/Y H:i:s' to your string depending on http://php.net/manual/en/function.date.php)

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