I have this simple function to subtract time: the input values are:
$current = '23:48:32';
$arrival = '23:41:48';
$time = date( "H:i:s", strtotime($current) - strtotime($arrival));
$waitingTime = $time; // 21:06:44
Looks like the diff for the minutes is correct, I am not sure why I am getting the 21 in front of the minutes. It should be 00:06:44.
Any help is appreciated.
Thank you.
try using gmdate()
$time = gmdate( "H:i:s", strtotime($current) - strtotime($arrival));
You can't expect this code to give you an interval.
the strtotime($current) - strtotime($arrival) line calculates a interval in seconds but when you pass it to date it assumes your speaking of an interval since epoch. so you get timezone translated value for $time; you must have gotten 9 because your probably behind UTC
use strtotime($current) - strtotime($arrival) / 3600 for hours and remainder divide by 60 for minutes. and then seconds
That's why PHP has DateTime & DateIntervals:
<?php
header('Content-Type: text/plain; charset=utf-8');
$current = '23:48:32';
$arrival = '23:41:48';
$current = DateTime::createFromFormat('H:i:s', $current);
$arrival = DateTime::createFromFormat('H:i:s', $arrival);
$diff = $current->diff($arrival);
unset($current, $arrival);
echo $diff->format('%H:%I:%S');
?>
Output:
00:06:44
This code echo 00:06:44!
$current='23:48:32';
$arrival='23:41:48';
$time = date( "H:i:s", strtotime($current) - strtotime($arrival));
echo $time;//00:06:44
What exactly is your problem?
Related
I am trying to get difference of two dates to count the duration of time.
This is my code:
<?php
$start_time = '2016-11-02 14:15:02';
$end_time= '2016-11-02 14:17:02';
$diff= strtotime($end_time) - strtotime($start_time);
$duration = date("H:i:s", $diff);
?>
So it is showing 05:32:00, but the actual result should be 00:02:00. I noticed that in result 05:30:00 added. I am not getting solution of it.
Use gmdate instead of date. It returns formated date string.
$duration = gmdate("H:i:s", $diff);
You need to set the defualt time_zone. I think you are using Indian time-zone . Thats why it is detting 5.30 hours more. Please see the below code
date_default_timezone_set("GMT");
$start_time = '2016-11-02 14:15:02';
$end_time= '2016-11-02 14:17:02';
$diff= strtotime($end_time) - strtotime($start_time);
$duration = date("H:i:s", $diff);
echo $duration;
You could use the DateTime Class to get the difference:
$d_start = new \DateTime('2016-11-02 14:15:02');
$d_now = new \DateTime('2016-11-02 14:17:02');
$interval = $d_start->diff($d_now);
Interval is an object which holds the difference in days, hours, minutes as public attribute (http://php.net/manual/de/class.dateinterval.php)
EDIT:
I just checked your code with some debug infos and it seems to work out of the box:
PHP File
$start_unix = strtotime($start_time);
$end_unix = strtotime($end_time);
var_dump($start_unix, $end_unix);
$diff = $end_unix - $start_unix;
var_dump($diff);
$duration = date("H:i:s", $diff);
var_dump($duration);
Console Output
php -f test.php
/tmp/test.php:9:
int(1478096102)
/tmp/test.php:9:
int(1478096222)
/tmp/test.php:12:
int(120)
/tmp/test.php:15:
string(8) "00:02:00"
i use ths method to find the difference between two timestamp and get the number of seconds between those two times, and i refresh the information with jquery like a counter.
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;
When the difference is more than 60 seconds, the $time comes back to 0, like a reset.
i would like to display 1 min XX s for example
The s flag for date() will never return a value greater than 59 as it only represents the current number of seconds of a given time which can never be more than 59 before rolling over into a new minute.
If you want the total number of seconds you can actually remove your second line of code as the difference between two Unix Timestamps is always in seconds:
$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;
If you want to display this as minutes and seconds you can use DateTime() which offers better tools for this:
$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');
format the date
$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;
Pass time like 1 & now 2
function diffrencePassTimeAction($DataTime){
$im = $DataTime - strtotime("now");
return $im;
}
Future time like 2 & now 1
function diffrenceFuturTimeAction($DataTime){
$im = strtotime("now") - $DataTime;
return $im;
}
this function delete (-less)
function diffrencePassTimeAction($DataTime){
if ($DataTime > 0)
return $DataTime - strtotime("now");
else
return strtotime("now"); // OR return 0;
}
Hello I try to take the difference between two dates and display it.
My problem is that the time difference I get is not the correct one.
This is my code:
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
echo date('H:i', $diffTime);
The result I get is:
02:05
The currect time should be this:
00:05
My guess that the date somehow takes timezone or something like this but Im not sure.
Thanks.
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
So what you're actually doing here is generating two UNIX timestamps (numbers) and then subtracting them. then you're passing the resulting number as if it were still a timestamp to date().
essentially $diffTime is the number of seconds between your two times. you could divide by 60 to get minutes, and so on and so forth, but PHPs DateTime objects are much better.
From the PHP docs:
http://pl1.php.net/strtotime
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
try this
<?php
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
echo round(abs($time1 - $time2) / 60,2). " minute"
?>
Below is the solution of date time in years,days.hours,minutes and seconds.
$time1 = strtotime('2014-03-28 15:20:00');
$time2 = strtotime('2014-03-28 15:15:00');
$diffTime = $time1 - $time2;
$y = ($diffTime/(60*60*24*365));
$d = ($diffTime/(60*60*24))%365;
$h = ($diffTime/(60*60))%24;
$m = ($diffTime/60)%60;
$s = ($diffTime)%60;
echo "Minutes - " .$m;
echo "<br/>";
I have something like that for example: 01:06:22 this represents 1hour, 6minutes and 22seconds. I want to take that, and multiple it by 6 and add it to some other hour such as 04:23 which is 4AM and 23Minutes not 4hours and 23 minutes.
Basically, as a result I expect that:
01:06:22
* 6 = 6hours 38minutes canceling the remaining seconds which are 12 in this case
Now, I want to take that and append it to other hour, 04:23 in this case, so the result would be:
11:01.
I have no clue how to start and do it, unfortunately.
Any help is appriciated!
Clarifications
The time that I have to multiple by 6 will never exceed 2 hours.
All the times are in the same format.
With DateTime it is simple:
$time = '01:06:22';
$dateSeconds = new DateTime("1970-01-01 $time UTC");
$seconds = $dateSeconds->getTimestamp() * 6;
$interval = new DateInterval('PT'.$seconds.'S');
$date = new DateTime('1970-01-01 04:23:00 UTC');
$date->add($interval);
echo $date->format('H:i:s');
Other solution with strtotime and gmdate. (Similar to Suresh but working):
$date = strtotime('1970-01-01 01:06:22 UTC');
$add = strtotime('1970-01-01 04:23:00 UTC');
$date = (($date*6)+$add);
echo gmdate('H:i:s', $date);
This is a solution if you want to implement it yourself.
The thing about timecode is that it can become really heavy with the if the if conditions etc if you don't do it right.
The best Way I thought of to deal with this is to convert everything to second.
so 01:06:22 would become:
numberOfSecond = 22 + 06 * 60 + 01 * 60 * 60
How to get the 22, 06 etc from the String? You can use Regex.
What you will need:
a function to extract the different values (hours, minute, second)
a function to convert the timecode into second
a function to convert back into timecode
the functions to multiply, add etc...
You might want to create a class for it.
You can try like this:
$date = strtotime('01:06:22');
$add = strtotime('00:04:23');
$date = ($date*6)+$add;
echo date('H:i:s', $date);
Note: Code is not tested.
First of all you want to multiply a time span by a factor. The easiest way to do this is to convert the span to seconds and do a straight multiply:
$date =DateTime::createFromFormat('!H:i:s', '01:06:22', new DateTimeZone('UTC'));
$seconds = $date->getTimestamp();
This code works by pretending that the time is a moment during the Unix epoch start so that it can then get the number of seconds elapsed since the epoch (the timestamp). That number is equal to the duration of the time span in seconds. However, it is vitally important that the input is interpreted as UTC time and not as something in your local time zone.
An equivalent way of doing things (as long as the input is in the correct format) which is lower-tech but perhaps less prone to bugs would be
list($h, $m, $s) = explode(':', '01:06:22');
$seconds = $h * 3600 + $m * 60 + $s;
Now the multiplication:
$seconds = $seconds * 6;
If you want to only keep whole minutes from the time you can do so at this stage:
$seconds = $seconds - $seconds % 60;
The final step of adding the result to a given "time" is not clearly specified yet -- does the reference time contain date information? What happens if adding to it goes over 24 hours?
Self explanatory :
$initialTime = '01:06:22';
$timeToAdd = '04:23';
$initialTimeExploded = explode( ':' ,$initialTime );
$initialTimeInMintues = ( $initialTimeExploded[0] * 60 ) + $initialTimeExploded[1];
$initialTimeInMintuesMultipliedBySix = $initialTimeInMintues * 6;
$timeToAddExploded = explode( ':' ,$timeToAdd );
$timeToAddExplodedInMintues = ( $timeToAddExploded[0] * 60 ) + $timeToAddExploded[1];
$newTimeInMinutes = $initialTimeInMintuesMultipliedBySix + $timeToAddExplodedInMintues;
$newTime = floor( $newTimeInMinutes / 60 ) .':' .($newTimeInMinutes % 60);
echo $newTime;
Result :
10:59
Let's say I got this time 21:07:35 now and this time into a variable 21:02:37 like this
<?php
$current_time = "21:07:35";
$passed_time = "21:02:37";
?>
Now I want check if $current_time is less than 5 minutes then echo You are online
So how can I do this in PHP?
Thanks
:)
To compare a given time to the current time:
if (strtotime($given_time) >= time()+300) echo "You are online";
300 is the difference in seconds that you want to check. In this case, 5 minutes times 60 seconds.
If you want to compare two arbitrary times, use:
if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online";
Be aware: this will fail if the times are on different dates, such as 23:58 Friday and 00:03 Saturday, since you're only passing the time as a variable. You'd be better off storing and comparing the Unix timestamps to begin with.
$difference = strtotime( $current_time ) - strtotime( $passed_time );
Now $difference holds the difference in time in seconds, so just divide by 60 to get the difference in minutes.
Use Datetime class
//use new DateTime('now') for current
$current_time = new DateTime('2013-10-11 21:07:35');
$passed_time = new DateTime('2013-10-11 21:02:37');
$interval = $current_time->diff($passed_time);
$diff = $interval->format("%i%");
if($diff < 5){
echo "online";
}
$my_time = "3:25:00";
$time_diff = strtotime(strftime("%F") . ' ' .$my_time) - time();
if($time_diff < 0)
printf('Time exceeded by %d seconds', -$time_diff);
else
printf('Another %d seconds to go', $time_diff);