What is a better way in the code below to add a new value to the variables $timestop and $time_diff if the condition is met?
//Calculates difference in time using 24h format
$timestart = strtotime("14:00:00");
$timestop = strtotime("07:00:00"); //if smaller value, it must end next day and meets the condition below
$time_diff = $timestop - $timestart; //elapsed time
if ($time_diff < 0 || $time_diff == 0) //if result is negative value, $timestop ends next day
{
$timestop = strtotime( '+1 day' , strtotime("07:00:00") ); //+ 1 day changes timestamp
}
/* UPDATED */
$time_diff = $timestop - $timestart; //added again
echo $time_diff;
No, overwriting variables is no problem, and it's daily routine.
You are not even overwriting in any other type, you're just resetting to another integer value.
It's even better, because you don't waste any extra memory space (which of course is not high, but think of it in a big scale).
Related
To convert a date to timestamp, I usually do this- strtotime ("2018-05-17 05:04:34) but now, I want to convert just hours (without date) e.g. 02:00:00 to timestamp. How do I do this?
Why I need this is to compare if a certain time is greater than the hour specified. This is what I am doing:
$reported = strtotime("2018-05-17 05:04:34");
$respons = strtotime("2018-05-17 17:04:34);
$response_time = $respons - $reported;
I want to be to check if $response_time is greater than 1 hour.
There is DateTime::diff, which probably does what you need
https://secure.php.net/manual/de/datetime.diff.php
In your case that should be
$datetime1 = new DateTime("2018-05-17 05:04:34");
$datetime2 = new DateTime("2018-05-17 17:04:34);
$interval = $datetime1->diff($datetime2);
echo $interval->format('H hours');
Strtotime has no problem parsing a time without a date.
No need to fake a date which will come back and bite you with daylight savings.
I also added a check to see if the start/end is "reversed".
$start= "2018-05-17 05:04:34";
$end = "2018-05-17 17:04:34";
//Note that it's intentionally reversed
$diff = strtotime(substr($start,11))-strtotime(substr($end,11));
//If the calculation was reversed add one day in seconds
if($diff <0) $diff += 86400;
If($diff >3600){
Echo "more than one hour";
}Else{
Echo "less than one hour";
}
https://3v4l.org/g1jZH
I believe only I have understood your question correctly.
I want to convert just hours (without date) e.g. 02:00:00 to timestamp.
There's no Date component here.
Okay, I assume they are of same date. If that's the case, just append an arbitrary date in front of the two to make the strtotime() function work:
$start = "05:04:34";
$end = "17:04:34";
$reported = strtotime("2018-05-17 " . $start);
$respons = strtotime("2018-05-17 " . $end);
$response_time = $respons - $reported;
if ($response_time > 3600)
echo "More than hour!";
else
echo "Less than hour!";
Note: This doesn't work if the start time is 17:00 and end time is say, 08:00 - which occurs in the next day. You have to make sure if the start time is greater than end time, then you have to add one more day to the end time.
I like the DateTime class, give this a try:
<?php
$reported = new DateTime('2018-05-17 05:04:34');
$reported->modify('+2 hours');
$now = new DateTime();
echo $now < $reported ? 'less than 2 hours' : 'more than 2 hours';
See it here https://3v4l.org/T7BEL
See the DateTime class docs here http://php.net/manual/en/class.datetime.php
I am trying to calculate the difference in time between two times using this:
round(abs(strtotime("17:30") - strtotime("18:30")) / 60,2);
= '1'
which works fine, but as soon as i make it over 2 days its not calculating correctly
round(abs(strtotime("17:30") - strtotime("02:00")) / 60,2);
= '15.5' this should be '8.5'
For more accurate and correct results you can use ->diff() function. As an example:
<?php
$val1 = '2014-03-18 10:34:09.939';
$val2 = '2014-03-14 10:34:09.940';
$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
Output:
-4 days
strtotime returns an timestamp. Currently your calculation is only partly correct, because you ignore negative values. In case of negative values (that should be the case, if the second time is on the next day), you should add 86400 (24*60*60 - the seconds of a day).
$start = strtotime("17:30");
$end = strtotime("02:00");
$diff = $end - $start;
// end date is on the next day
if ($diff < 0) {
$diff += 86400;
}
$hours = $diff / 3600;
echo round($hours, 2);
You can do the math yourself by converting dates into unixtime:
strtotime('2017-12-29 02:00')-strtotime('2017-12-28 17:30')
This will return the difference in seconds, so if I want to print the value in hours, I have to divide by 60 twice:
php > print((strtotime('2017-12-29 02:00')-strtotime('2017-12-28 17:30'))/60/60);
8.5
I have two times like start_time and end_time and also having a time range like a start time and a end time. I want to check start_time and end_time are in the range of start time and end time or not. How to check that.
$start_time1 = 10:15 am; //! time table start time
$end_time1 = 12:30 pm; //! time table end time
$strattime2 = 10:00 am; //! time range - start time
$endtime2 = 1:00 pm; //! time range - end time
How to resolve this problem?
Just check the boundary of the range. The query must be started on or after the range and must be ended on or before the range ends. So
function check($queryStart, $queryEnd, $rangeStart, $rangeEnd) {
return ($queryStart >= $rangeStart && $queryEnd <= $rangeEnd);
}
If you want to check whether the query is overlapping the range or not, you should check whether the query ends before the range start or the query starts after the range end.
function overlap($queryStart, $queryEnd, $rangeStart, $rangeEnd) {
return !($queryEnd < $rangeStart || $queryStart > $rangeEnd);
}
you may convert all the times to DateTime() Objects and then check the difference like so:
<?php
function startStopTimeIsWithinRange($startTime='10:15', $stopTime='12:30') {
$dateStart = new DateTime('2016-10-30 ' . $startTime); //<== IGNORE, THE DATE. NOTICE THE TIME
$dateStop = new DateTime('2016-10-30 ' . $stopTime); //<== IGNORE, THE DATE. NOTICE THE TIME
$rangeStart = new DateTime('2016-10-30 10:00'); //<== IGNORE, THE DATE. NOTICE THE TIME
$rangeStop = new DateTime('2016-10-30 13:00'); //<== IGNORE, THE DATE. NOTICE THE TIME
if($dateStart >= $rangeStart && $rangeStop >= $dateStop){
return true;
}
return false;
}
var_dump( startStopTimeIsWithinRange('10:15', '12:30') ); //<== NOTICE THE COLON (:) AND NOT DOT (.)
I'm just want to check if my way is correct or not. I want to calculate the remaining time for an event.
There are 3 component for it.
The start datetime (in mysql datetime), which is retrieved from mysql.
The duration (minutes in integer), which is also retrieved from mysql.
The current datetime, which is retrieved from php function now().
To count:
The remaining time (in time hh:mm:ss), which is from formula (start + duration) - now
My propose code is:
$row = data->fetch_assoc();
$start = $row["start_time"]; // e.g: "2015-06-19 09:37:16"
$duration = $row["duration"]; // e.g: 60 (minutes)
$now = time(); // e.g: 1434648994
$start_dt = strtotime ($start);
$remaining = ($start_dt + $duration * 60) - $now;
echo "Remaining time: ".$remaining. " seconds."
Is this correct?
Your code now calculates time until the end of the event.
And if $start_dt is lower then $now you get negative value.
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);