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/>";
Related
I have 2 times in my sql table like 02:56:07pm and 03:56:14pm, so i converted it like:
$time1 = date("G:i:s", strtotime($row['timein']));
$time2 = date("G:i:s", strtotime($row['timeout']));
which gives me two times like 14:56:07 and 15:56:14
now am trying to find the difference in hours between this two times and I did like below:
$difference = round(abs($time2 - $time1) / 3600,2);
echo $difference;
but here am getting 0 as answer
This works for me:
Using strtotime that converts a date/time string into a Unix Timestamp (eg. 163612416703). Now calculations can be done on the times.
$time1 = strtotime('02:56:07pm');
$time2 = strtotime('05:56:14pm');
$difference = date('H:i:s', ($time2 - $time1));
echo $difference; //03:00:07
Play around with the formatting etc..
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 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
I'm struggling to calculate time difference between two 24 hour format. The data extracted from my database is like this:
$timein = "09:00";
$timeout = "17:00";
$timediff = $timein - $timeout
As above, how do I get the $timediff = 8?
$timediff = date("H",strtotime($timein) - strtotime($timeout));
You can also change to format of the result,
check: http://php.net/manual/en/function.date.php
If you first set them to full dates:
$timein = "09:00";
$timeout = "17:00";
$datein = strtotime(date("Y-m-d ".$timein);
$dateout = strtotime(date("Y-m-d ".$timeout);
So, in fact the date you are using (todays date) can be any other date given since it's all about the time between the hours. Then you can create the php date function:
$hourIn = date("G", $datein); // = 9
$hourOut = date("G", $dateout); // = 17
Followed by simple math:
$diff = $hourOut - $hourIn; // = 8
You can use strtotime() for time calculation.
$timein = strtotime('09:00');
$timeout = strtotime('17:00');
$timediff = $timein - $timeout;
echo 'Time: '.date('H:i:s', $timediff);
Break the problem down into two steps.
Convert each time into a number of seconds since midnight.
Calculate the difference between two times.
This breaks the larger problem down into basic problems which can be solved more easily. It is a very valuable tool in the programmer's belt.
For the second part, be sure to handle midnight the way you need to for your data. Perhaps if the timeout is before the timein, then you'll need to add 24 hours to the total? That's just a guess, because I do not know your data.
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.