This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 4 years ago.
I have 2 dates in mysql:
2018-07-13 13:00:00 - data
2018-07-14 16:01:00 - godzina
$timestamp1 = strtotime($data['data']);<br>
$timestamp2 = strtotime($data['godzina']);<br>
$time_difference = $timestamp2 - $timestamp1;
$time_total = ($time_difference/3600);
echo "td".$time_total."/td";
Now it returns: 27.016666666667
How to change it to: 27:01 in table?
You can simply use DateTime->diff method to get the interval between those 2 dates:
$date1 = '2018-07-13 13:00:00';
$date2 = '2018-07-14 16:01:00';
$interval = (new DateTime($date2))->diff(new DateTime($date1));
$totalHours = $interval->days * 24 + $interval->h;
echo sprintf("%02d", $totalHours) . ':' . sprintf("%02d", $interval->i);
Related
This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 3 years ago.
I got some problems handling string and time.
I am reading a form which gives me a string like this: "08:00"
Now i am running a foreach loop after which i want to add e.g. 15 minutes to the upper string.
I tried to convert the "08:00" to a time with
$string = "08:00";
$time = date("H:i", strtotime($string));
echo $time; //echos 1577260800
How can i add e.g. 15 minutes or even better a string like $add = "10" to the $time? The following doesnt work.
$add = "10";
$newtime = $time + strtotime($add);
Just add time in seconds to an existing time.
$string = "08:00";
$timeInSeconds = strtotime($string) + 15*60; // 15*60 => 15 minutes in seconds
$time = date("H:i", $timeInSeconds );
echo $time; // shows 8:15
You can just use the strtotime's parsing of words.
Meaning you can just as you ask "add 10 minutes".
$string = "08:00";
$time = date("H:i", strtotime($string . " +10 minutes"));
echo $time; //8:10
This question already has answers here:
Create Variable in PHP Equal to Current Time Minus One Hour
(8 answers)
Closed 3 years ago.
how to get 1 hours before in php
I try with:
but not working
$jam = "10:00:00";
$1hrsbefore = strtotime($jam), strtotime(-1 hours);
Try this code :
$jam = "10:00:00";
$timeadd= strtotime($jam) - 60*60;
$time = date('H:i:s', $timeadd);
echo $time;
Try this
$jam="10:00:00";
$time = date('H:i:s', strtotime($jam) - 60*60);
echo $time;
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I get the string data from the database
The data format like:
07:00:00.0000000
It means 7:00 am.
How can i convert this string into Date type for comparison.
Therefore, I can get 2 data. Such as
Start:
07:00:00.0000000
End:
16:30:00.0000000
After the time comparison, i can got a answer like 9.3.
You can use strtotime :
$start = strtotime("07:00:00.0000000");
$end = strtotime("16:30:00.0000000");
$difference = $end - $start;
Or with DataTime object :
$start = new DateTime("07:00:00.0000000");
$end = new DateTime("16:30:00.0000000");
$interval = $start->diff($end);
$difference = $end->getTimestamp() - $start->getTimestamp();
Then echo the result :
echo $difference; // in seconds
echo $difference / 60; // in minutes
echo $difference / 3600; // in hours
echo $interval->format('%s') // in seconds
It's then up to your preference. I also suggest you to have a look at this post regarding the performances of the two solutions.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>
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.