This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I am trying to check if pass 5 minutes between two dates.
Here is my code:
$time = strtotime("+5 minutes").'<br>';
$var = '1518219956';
$e1 = date("d-m-Y H:i",$time);
$e2 = date("d-m-Y H:i",$var);
if($var > $time){
echo 'true<br>';
echo 'Time: '.$e1.'<br> Var:'.$e2;
} else{
echo 'false<br>';
echo 'Time: '.$e1.'<br> Var: '.$e2;
}
I am sorry but I lost myself with this timestamps..
Sorry, I don't mean to be rude but I was confused by the question. What I designed here was to see if time B is five minutes after Time A.
A few notes:
No need to bother with strtotime or date. Just keep everything in unix time. Compare the seconds by using 60 * 5. 60 for 60 seconds in a minute and 5 for the number of minutes.
<?php
//Time A
$timeA = '1518223062';
//Time B (Which I set at the current time)
$timeB = time();
//five minutes in seconds
$fiveMinutes = 60 * 5;
//check if current time is after 5 minutes the initial time
if ( ($timeA+$fiveMinutes) <= $timeB) {
echo "True";
}
else {
echo "False";
}
?>
Related
This question already has answers here:
AM/PM comparison in PHP [duplicate]
(3 answers)
Closed 4 months ago.
time() function in PHP used to get current time.But there's a problem!
Suppose we get the current time 12:05 AM (EST) using this function, then if i compare this time with another time(For example,10:50 AM),obviously PHP will consider that the timestamp of 10:50 AM is greater than 12:05 AM)..but actually it's wrong!12:05 is basically 00:05 AM...!!!
I don't know how to compare times like this accurately,any help would be appreciated.
I'm using this block of code to compare those two times...
$current_time = time(); //suppose current time is 12:15 AM(returned by time() func)
$time1 = '1:30 AM';
$draw_time = strtotime($time1);
if($current_time < $draw_time1) {
echo 'Perfectos!';
}
$current_time = time();
$time1 = date('d.m.Y') . ' 11:42 AM';
$draw_time = strtotime($time1);
echo ($draw_time) . " " . $current_time . "\r\n";
if ($current_time < $draw_time) {
echo 'True';
} else {
echo 'False';
}
You can try this way
Your code is incorrect , bacause you get first variable for only hour and second variable for whole time
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:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;
Difference is a MySQL time. I am trying to check if the MySQL time returned is greater than 5 minutes. I have tried the code below but it doesn't seem to be working.
if (strtotime($myMySQLTimeValue) > strtotime("+5 minutes",)) {
// My code if MySQL time is greater than 5 minutes
}
Any ideas?
If you already have the time in the format 00:06:43 then you could use the following code
$diff_time = "00:06:43";
$diff_arr = split(":", $diff_time);
// Check mins and hours
if ( intval($diff_arr[1]) >= 5 || intval($diff_arr[0]) > 0) {
echo "Time more then 5 min";
} else {
echo "Time less then 5 min";
}
Assuming you retrieved mysql time by "SELECT NOW()", you can use below comparison:
$_5minuteslater = time() + 5 * 60;
if ($mysqlTime > $_5minuteslater) {
}
My goal is to compare the current time with the last visited time and if was 5 minutes ago then allow to visit again, else, deny! Here what I have so far
$last_activate = strtotime($last_activ); //$last_activ is TIMESTAMP value retrieved from MySQL database
$current_time = strtotime('now');
if(($current_time - $last_activate) > strtotime('5 minutes')){
//allow access
}
else{
//deny access
}
At the moment, the code above always executes else statement even if $last_activate was 24 hours ago. Anyone knows what point I am missing?
strtotime('5 minutes') means now + 5 minutes. Proof:
echo date("Y-m-d H:i:s", strtotime('5 minutes'));
2013-03-21 19:41:27
Do this instead:
if(($current_time - $last_activate) > 5 * 60){
Replace 5 minutes with 5 minutes ago.