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"
Related
I have 2 datetime , and I wanna to calculate difference in minutes :
$date_reservation=strtotime($_POST['date_reservation']);;
$dt = new DateTime();
$date_annulation = strtotime($dt->format('Y-m-d H:i:s'));
$attente = (round(abs($date_annulation - $date_reservation)/60)%60);
It only take the difference between minute without hours .
I've tried this function ( from php documentation )
$interval = $date_annulation->diff($date_reservation);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
But it dosn't work (error 500)
Your problem is that $date_annulation is not a DateTime object. Keep it simple using strtotime
<?php
$date_reservation = strtotime($_POST['date_reservation']);
$date_annulation = strtotime('now');
$diff_minutes = ($date_annulation - $date_reservation)/60;
echo $diff_minutes;
A response of 500 Internal Server Error means there is a problem in your PHP code. Check the server log files (Apache's error_log, PHP's php_errors.log) to find out the exact place (file and line) and the cause.
In the meantime, the code you copied from the documentation doesn't work because you tried to call a method of the DateTime class on a number (the value returned by strtotime(). It doesn't work this way.
It does work, however, if you use DateTime (and related) objects:
$date_reservation = new DateTime($_POST['date_reservation']);
$date_annulation = new DateTime('now');
$interval = $date_annulation->diff($date_reservation);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
If you are aware of the possible inaccuracies using this approach, you can just compare the Unix timestamps:
$date_reservation = strtotime('2016-05-20 13:30');
$date_annulation = strtotime('now');
$diff_minutes = round(abs($date_annulation - $date_reservation)/60, 0);
The only problem in your code is, that you mod 60 your minutes (%60). This of course gives you only the minutes above full hours.
Please note that strtotime interprets the datetime string using the time zone set on the machine running this code. You can set in your code using date_default_timezone_set().
See an example on ideone.com.
i give you code of time difference
<?php
//$datetime1=strtotime('Y-m-d','time to which difference obtained');
$current_time =strtotime(date("Y-m-d H:i:s"));
//$checkTimeEnd = strtotime('time to which difference obtained');
$checkTimeStart = strtotime('time to which difference obtained');
//echo $current_time;
$all = round((($current_time - $checkTimeStart) / 60),2);
//echo floor($all/(60*60*24));
$test = round($all/60,2);
$d = floor ($all / 1440);
$h = floor (($all - $d * 1440) / 60);
$m = $all - ($d * 1440) - ($h * 60);
?>
print $d for date $h for hours $m for minutes
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 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?
I'm looking to create a countdown timer in PHP. When a user clicks a button it saves the current date & time into a database entry, then it should take the difference of that entry with the current date and time and 'doSomething' when the difference is larger than 48 hours.
My issue is with the actual countdown.
I've tried the following but to no avail it only counts the difference of the of both strings and doesn't take the days in account. Not only that but it also appears to show the resulted difference incorrectly:
$d1=strtotime("2012-07-08 11:14:15");
$d2=strtotime("2012-07-09 12:14:15");
$diff = round(abs($d1 - $d2));
$cd = date("H:i:s", $diff);
echo $cd;
Thanks for helping me Yan.kun from StackOverflow! The code submitted below was the solution! In order to display strictly the countdown in hours:minutes:seconds I replaced the printf() code with the following:
$hours = ($result->d*24)+$result->h;
$minutes = $result->i;
$seconds = $result->s;
echo $hours . ":" . $minutes . ":" . $seconds;
Try this instead:
$d1 = new DateTime("2012-07-08 11:14:15");
$d2 = new DateTime("2012-07-09 12:14:15");
$result = $d1->diff($d2);
printf('difference is %d day(s), %d hour(s), %d minute(s)', $result->d, $result->h, $result->i);
EDIT:
And if you have no PHP 5.3 available, you can convert your times into an unix epoch timestamp like described in this answer.
not sure if this is what you want:
$d1=strtotime("2012-11-18 11:14:15");//2 days earlier
$d2=strtotime("2012-11-20 11:14:15");//today
$diff = $d2 - $d1; //difference in seconds.
$hours = $diff/60/60;//translation to minutes then to hours
echo $hours;
if($hours>48){
echo "Script";
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
I have two dates
$departure_Dtae=2012-07-25T07:30:00
$arrival_date =2012-07-25T10:25:00
T means Time i.e. in 2012-07-25T10:25:00 date is 2012-07-25 and the time is 10:25:00 (hr: mts: s)
I need to find out the total hrs between these two times
I.e. in this case the total hour is 2 hr and 55 minutes
But I don't know how I calculate this in PHP
Does anyone know this?
If using PHP Version 5.3+ then you can use DateTime::diff():
<?php
function getDiff($strStart, $strEnd) {
$start = DateTime::createFromFormat("Y-m-d G:i:s", $strStart);
$end = DateTime::createFromFormat("Y-m-d G:i:s", $strEnd);
return $start->diff($end)->format('%hhrs %imins and %sseconds ');
}
var_dump(getDiff('2012-07-25 07:30:00', '2012-07-25 10:25:00'));
its simple
$departure_Dtae="2012-07-25T07:30:00";
$arrival_date ="2012-07-25T10:25:00";
$departure_Dtae= str_replace("T", " ", $departure_Dtae);
$arrival_date= str_replace("T", " ", $arrival_date);
$diff= (strtotime($arrival_date)-strtotime($departure_Dtae));
echo date("h",$diff);
Try this :
$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";
$end = date("h:i:s",strtotime($end_time));
$start = date("h:i:s",strtotime($start_time));
$diff = strtotime($end) - strtotime($start);
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds
//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes
$total_visit = $convert_hr.":".$remainder;
You can use the DateTime object in php, which has loads of methods for manipulating dates.
DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php and DateTime::diff http://www.php.net/manual/en/datetime.diff.php would be the functions you would need to perform this task.
$date1 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 12:45');
$date2 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 13:45');
$interval = $date1->diff($date2);
echo $interval->format('H:i');
strtotime(date($arrival_date)) - strtotime(date($departure_date));
Will give you the time diff in secounds, then you can manipulate that as you wish.