PHP: Check if MySQL time value is greater than 5 minutes - php

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) {
}

Related

PHP check if pass 5 minutes between two dates [duplicate]

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";
}
?>

PHP - Deduct two timestamps from each other, display in minutes

I have a mySQL database that stores the checkin and checkout time of a person in a gym. I have imported the checkin and checkout times in to my PHP script. Now I want to deduct the two timestamps from each other - giving me the time left. I want this to display in minutes.
This is my idea:
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
echo ($checkIn - $checkOut);
// I want this to display 31 minutes.
I have seen many examples on StackOverflow, but none matched my description and I couldn't reverse engineer the ones I found - because they use the time() function - which I guess takes the current time.
You can use strtotime();
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
echo date("i", ($checkIn - $checkOut)) . 'Minute(s)';
IMP Note: The above method will only work if the minutes are below 59, or else the hours will be rounded off and discarded. So if your requirements is showing the time in minutes which can be grater than 59 minutes eg. 144 minutes, then you'd want to just divide by 60, as follows.
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
$seconds = $checkOut_stamp - $checkIn_stamp;
if($seconds > 0){
if($seconds > 60){
$minutes = $seconds/60;
} else {
$minutes = 0;
}
} else {
$minutes = 0;
}
echo $minutes . ' Minute(s)';
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
$time = (strtotime($checkIn) - strtotime($checkOut));
echo date('i',$time);
use this code
If you were fetching this record from database then you can simply achieve it using MySql function TIMESTAMPDIFF, as no need to use PHP function over here
Select TIMESTAMPDIFF(MINUTE,checkIn,checkout) as tot_diff from your_table

Return the difference between two (dates and time ) PHP

i tried most of what is available on stack overflow but none seem to work.
any way i am trying to compare two (date and time formats). and calculate whether their difference is within 5 seconds of each other
the first date is the current date:
$today = date("Y-m-d H:i:s");
the second date is taken from mysql database:
$result = mysql_query("SELECT * FROM BUS_DATA where BusRegID = 'bus'") or die(mysql_error());
$row = mysql_fetch_array($result);
$update_date = $row["update_date"];
the answer should be segmented into years , month , days , hours , minutes ,and seconds portions.
I am running PHP Version 5.3.3
Edit: most answers give result in time frame only , I want to check whether the date matches , then compare if the time of the day is within 5 seconds , than you guys in advance :)
Try this
function getTimes($t1, $t2)
{
$timeFirst = strtotime($t1);
$timeSecond = strtotime($t2);
$differenceInSeconds = $timeSecond - $timeFirst;
$h=0;
$m = floor($differenceInSeconds / 60);
$s = $differenceInSeconds % 60;
if ($m>=60)
{
$h = floor($m / 60);
$m = $m % 60;
}
$tim = $h.':'.$m.':'.$s;
return $tim;
}
it will return difference time in hours:min:sec
use strtotime, it's understands almost any date formats:
$timeDiff = abs(strtotime($today) - strtotime($update_date));
this gives you the difference between dates in seconds.
If you want to know whether it's within 5 seconds then use Timestamps, makes it into a simple int calculation.
$now = time();
$test = strtotime($update_date);
if($now - $test > 5) {
//NOT WITHIN 5 SECONDS
}

Countdown for 7 hours then show ok

How can I countdown for 7 hours from a variable time (I will get time from my table which is inserted with timestamp), after 7 hours from variable time I will update a table.
I need something like that
$time = 2013-05-18 02:00:00 // comes from database
$target = $time + 7hours // time from database +7hours will be 2013-05-18 09:00:00
$until = $target - $time
I need something like below code
if ($until > 0 ) {
echo "you need to wait for $until hours"
} else {
echo "time is ok"; // i will update a table
}
Convert time into string using strtotime($time)+25200 where 7 hour =60*60*7=25200 sec and then check and also add this file to your cron job.
So, considering that $database_time is the stored time, in your db, and $time_now is your computers time, this message below, just echoed out:
You Must wait 4 hours right, now
It could be done much better, but still calculates the hours from now, to db and tells you how much more, you must wait :)
<?php
$date = date_create();
$database_time = '2013-05-18 22:00:00';
$time_now = date_format($date, 'Y-m-d H:i:s');
$check = $database_time[11];
$check .= $database_time[12];
$check2 = $time_now[11];
$check2 .= $time_now[12];
$time_left = $check - $check2;
So, here is how you can manage your ourputs
if($time_left > 0) {
echo "You Must wait $time_left hours right, now";
}else{
echo "Time is OK";
}

Strtotime not working as expected

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.

Categories