how to check if time is more than 30minute? - php

I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.
I would need some help fixing that code.
$current = "08:05";
$to_check = date('h:i');
if($to_check > $current + strtotime('30 minute')){
echo "greater";
}
else {
echo "not greater";
}

First, your $current isn't the current time, $to_check is, so your variable names are misleading.
That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.

$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
echo "Time's up!"
else
echo "time has not expired";

This should work for you:
<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes'));
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB
if($to_check > $current){
echo "greater";
}
else {
echo "not greater";
}

Save time using time function this would give you time in seconds from 1970 so for example current time is 1501137539 and after 30 minutes it would be (1501137539 + 30*60) so you would just need to check if difference b/w current time and stored time is greater that 30*60 then half an hour is over.

Try this snippet:
function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
$to_check = strtotime($to_check);
$current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
return ($current>=$to_check) ? true : false;
}
Or go pro, and have a custom "seconds since", just because you can?
function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
$to_check = strtotime($to_check);
$current = time() + $seconds; //We add x seconds
return ($current>=$to_check) ? true : false;
}
Example usage:
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
print "Is within 30 minutes.";
else:
print "IS NOT within 30 minutes";
endif;

Try this to solve the issue
$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));
$newtime = round(( $to_check - $current ) /60 );
if($newtime > 30){
echo "greater";
}else {
echo "not greater";
}

Related

Check if date() is greater than a specific time

In my code I pretty much send a token to the database with the
date('Y-m-d H:i:s');
function. But I am checking to see if the timestamp I sent if greater than 60 seconds if so i echo yes and else no. I keep getting no and I know its more than a minute because i time it. I've seen post about this but they're using specific dates and I am just going on when a user submits a form and checking if the token is 60 seconds old. Here is my code
php
<?php
require_once('db_login.php');
$stmtToken = $handler->prepare("SELECT * FROM email_token");
$stmtToken->execute();
$rowToken = $stmtToken->fetch();
$date = $rowToken['time_stamp'];
if($date > time() + 60) {
echo 'yes';
} else {
echo 'no';
}
?>
You can also play with dates in different manners. All four lines here are equivalent:
$now = (new \DateTime(date('Y-m-d H:i:s')))->getTimestamp();
$now = (new \DateTime('now'))->getTimestamp();
$now = (new \DateTime())->getTimestamp();
$now = time();
And then you can compare in this manner:
$tokenExpirationTimestamp = (new \DateTime($date))
->modify('+60 seconds')
->getTimestamp();
$isTokenExpired = $tokenExpirationTimestamp < time();
if ($isTokenExpired) {
// ...
}
When you compare times and dates you can either use datetime or strtotime.
Using strings will not work as expected in all cases.
In comments you mentioned how you want to compare, and you need to add the 60 seconds to the "date", not the time().
if(strtotime($date) + 60 < time()) {

how to calculate 2 hours from the time request made in seconds

My server time is GMT, so I convert it to Asia/Kuala Lumpur timing.I need to calculate time difference between current time and the time request was made. The time request was made is stored in database and retrieved in $reset_req variable.
$reset_req="2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
}
echo "current time= ".strtotime(time());
echo"<br/>";
echo "time req made=".strtotime($request_date2);
echo"<br/>";
$timediff = strtotime(time()) - strtotime($request_date2); // in seconds
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
Everyone already said how to calculate 2 hours (2* 3600sec). But what they didn't tell you is that you can't do this: strtotime(time()); time(); already gives you a Unix timestamp so you can't convert it twice.
Your Code should look more like this
<?php
$reset_req = "2015-06-30 11:30:23";
$timezone_offset = +8; // us central time (gmt-6) for me
if(isset($reset_req)){
$request_date2 = strtotime($reset_req)+$timezone_offset*60*60;
$current = time();
} else {
echo '$reset_req was not set';
exit;
}
echo "current time= " . $current . "<br />";
echo "time req made=" . $request_date2 . "<br />";
$timediff = $current - $request_date2; // in seconds
if($timediff < (2*3600))//how to calculate 2 hours here
{
echo "less than 7200 sec have past since $request_date2. Past: $timediff seconds";
}else{
echo "more than 7200 sec have past since $request_date2. Past: $timediff seconds";
}
?>
$hour = 3600; //an hour has 3600 seconds
if($timediff < 2 * $hour )
{
//do something
}
$timediff is in seconds. So you have to convert that to hours or convert the 2 hours to seconds and then need to compare like as follows:
replace
if($timediff < 2 hours)//how to calculate 2 hours here
{
//do something
}
with
if($timediff < (2*3600))//how to calculate 2 hours here
{
//do something
}
Try this
<?php
$reset_req = "2015-06-30 11:30:23";
//set default timezone here
date_default_timezone_set("Asia/Kuala_Lumpur");
$request_date2 = '';
if(isset($reset_req))
{
$request_date2 = strtotime($reset_req);
}
echo "Current time = ".time();
echo"<br/>";
echo "Time req made =".$request_date2;
echo"<br/>";
$timediff = time() - $request_date2; // in seconds
echo "<br>Time Differnce : ".$timediff;
//how to calculate 2 hours here
if($timediff < 2*3600)
{
//do something
}
?>

how to calculate time left using string minus another string?

i have lets say a $value = 5; and the valnue means 5 minutes, and i have a file saved on the server and getting modified a lot called check.txt i want a code to do a calculation of if timenow - timemodification of file <= 0 in H:i:s from the main $value of 5 minutes then continue, else echo please wait minutes left from the time now - filetimemodification of the main value of 5 minutes = $timeleft in m:s format.
i'm testing on the current code but i keep getting a value of -1376352747
my code which is know is bad :) is
$filename = 'check.txt';
$time = date("H:i:s");
$time = str_replace("00", "24", $time);
$filemodtime = filemtime($filename);
$timeleft = $time - $filemodtime;
$h = explode(':', $time);
$h = $h[0];
$h = str_replace("00", "24", $h);
$m = explode(':', $time);
$m = $m[1];
$s = explode(':', $time);
$s = $s[2];
$hms = ("$h:$m:$s");
if (count($filemodtime - $time) <= 0) {
echo "you can continue";
}
else {
echo " please wait $timeleft";
}
thanks in advance
The filemtime() function returns a UNIX-timestamp in seconds, and the time() function returns the current time as a UNIX-timestamp. So by using that difference, you get the file's age in seconds.
$age = time() - filemtime($filename);
// if older then 5 minutes (5 * 60 secounds)
if($age > $value*60)
{
// good
}
else
{
$time_left = $value * 60 - $age;
$time_left_secounds = $time_left % 60;
$time_left_minutes = ($time_left - $time_left_secounds) / 60;
$formated_time_left = sprintf("%02d:%02d", $time_left_minutes, $time_left_secounds);
echo "Please wait {$formated_time_left}";
}
I would recommend to work with time() rather than date().
that way, you can substract the file time from the current time() function, and see if it is bigger than 5 minutes * 60 seconds.
Good luck!

php within x seconds of y time

How can I make a script that can check wether it is currently x seconds from 12am or 12pm?
thanks
You have to get the current timestamp, using the time() function.
Then, you have to get the timestamp of 12am, using for example the strtotime() function.
Then, substract those two values ; and if the absolute value of the result is X, then it's the right time for you ;-)
<?php
$noon = strtotime( "noon" );
$midnight = strtotime( "midnight" );
$timeToMidnight = $midnight - date();
$timeToNoon = $noon - time();
?>
<?php
function timeDiff($firstTime,$lastTime)
{
// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;
// return the difference
return $timeDiff;
}
//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");
?>
<?php
$time = time();
if( (date("g", $time) % 12) * 3600
+ preg_replace('/^0/', '', date("i", $time)) * 60
+ preg_replace('/^0/', '', date("s", $time))
> $selected_time) {
// do whatever
}
?>
This should work... right?
$time = time();
if ((date("g", $time) == 12) && (date("i", $time) < 10)) {echo 'W00T!';}

PHP date comparison

How would I check if a date in the format "2008-02-16 12:59:57" is less than 24 hours ago?
if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }
Just adding another answer, using strtotime's relative dates:
$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
// Do something
}
I think this makes the code much more readable.
if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
// less than 24 hours ago
}
e.g. via strtotime and time().
The difference must be less then 86400 (seconds per day).
<?php
echo 'now: ', date('Y-m-d H:i:s'), "\n";
foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
$diff = time()-strtotime($input);
echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
}
prints
now: 2009-12-03 18:02:29
2008-02-16 12:59:57 56696552 -
2009-12-02 13:00:00 104549 -
2009-12-02 20:00:00 79349 +
only the last test date/time lays less than 24 hours in the past.
Php has a comparison function between two date/time objects, but I don't really like it very much. It can be imprecise.
What I do is use strtotime() to make a unix timestamp out of the date object, then compare it with the output of time().
Just use it.....
if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}
Maybe it will be more easy to understand...
$my_date = '2008-02-16 12:59:57';
$one_day_after = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));
if($my_date < $one_day_after) {
echo $my_date . " is less than 24 hours ago!";
} else {
echo $my_date . " is more than 24 hours ago!";
}
There should be you variable date Like
$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
You can Compare both date after convert date into time-stamp so :
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
OR
if($current_date_time_stamp >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
You can use Simple PHP to do this:
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();

Categories