$timenow = time();
if($lasttime - 120 > $timenow)
Is this right? to check if there has been 2 minutes ( 120 seconds ) since lasttime?
It's either $lasttime + 120 > $timenow or $timenow - 120 > $lasttime.
Imagine both times start with 0 and the "timenow" grows with every second. With that mindtrick you should get it.
if (($lasttime+120)>=time())
I suggest you to calculate number of elapsed seconds first, then compare it to your needs:
$elapsed_time = time() - $lasttime; ## time() is increasing
if ($elapsed_time > 120) {
## more then 2 minutes passed
}
else {
## it's not time yet
}
This should help to understand your code later.
Use
$time_start = microtime(true);
//Do whatever
if ($time_start-microtime(true)>=2000){
//Yup, 2 minutes
}
Related
I have a php script where I need to make sure a pre-set "future" time has not passed.
When the time is originally logged (or passed and needs relogged), I am taking:
$newTime = time() + 15000; // 2.5 minutes from "now"
The system is tossing this in the DB no problem and the numbers appear to be correct.
Now, when the page is loaded, it pulls the number from the DB and loads it into the .php file:
error_reporting(E_ALL);
ini_set('display_errors',1);
$tname = $_SESSION['username']."Data";
$results = $conn->query("SELECT val FROM $tname where pri='pettyTimer'") or die(mysqli_error($conn));
//$conn declared elsewhere for connection and does work properly
$row = $results->fetch_assoc();
$timer = $row['val'];
I am then comparing the times:
$now = time();
if ($timer > time()) { //script below
} else {
//more script that seems to be working fine
}
When the original conditional $timer > time() is true I am trying to break down the minutes and seconds of the time remaining and echoing them in a basic format that is readable to the user:
$raw = ($timer - $now);
$minutesLeft = floor($raw / 60000);
$totalMinutes2Mils = $minutesLeft * 60000;
$totalRemainingSecs = round(($raw - $totalMinutes2Mils) / (1000));
echo "You are still laying low from the last job you ran. You still have ".$minutesLeft." Minutes and ".$totalRemainingSecs." Seconds left.";
My problem is, the time does not appear to be shifting when I refresh/reload the page.
I echoed both time() and $timer and they are 15000 milliseconds apart when I first loaded it, so this should only exist (conditional be true) for about 2.5 minutes, but I've been working at least 5 minutes since my last set and it's still at 14 seconds.
Can someone please double check my math to make sure I'm calculating this correctly? Thanks!
The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
http://www.w3schools.com/php/func_date_time.asp
You are treating it as milliseconds, but should be treating it as straight seconds. take about /1000 and you should be ok.
$minutesLeft = floor($raw / 60);
$totalMinutes2Mils = $minutesLeft * 60;
$newTime = time() + (60*2.5); // 2.5 minutes from "now"
time() returns seconds, not milliseconds, so you should add 150 instead of 15000 to get 2:30 minutes.
I need to get the remaining seconds in the actual hour for caching weather-data. What's the most efficient method of doing that?
Simply use PHP's time function as follows:
<?php
function getSecondsRemaining()
{
//Take the current time in seconds since the epoch, modulus by 3600 to get seconds in the hour, then subtract this from 3600 (# secs in an hour)
return 3600 - time() % (60*60);
}
?>
This should get you the performance you want.
Here you go mate:
$currentMinute = date("i");
$minuteLeft = 60 - $currentMinute;
$secondLeft = $minuteLeft * 60;
$secondLeft += date("s");
echo $secondLeft;
How about
$date = new DateTime("now");
$seconds_left = ( ( 59 - $date->format("i") ) * 60 ) + ( 60 - $date->format("s") );
I have a time() value saved in a variable like this:
$latest_attempt = 1337980678;
I am trying to calculate some delay.
$remaining_delay = time() - $latest_attempt - $delay;
However the result of $remaining_delay is increasing when I update the browser, and not the way around.
"You must wait 95 seconds before your next login attempt"
If I update some seconds later "You must wait 102 seconds before your next login attempt"
It's doing the opposite what it should doing, instead it would rather decrease than increase. What have I done wrong? I believe I need to do something with latest_attempt variable, but I could not find anything i the php manual.
I'd say, something like this:
$remaining_delay = $latest_attempt + $delay - time();
$time_since_last = time() - $last_attempt;
if ($time_since_last <= $delay) {
$remaining = $delay - $time_since_last;
} else {
... good to go ... delay's expired
}
The remaining delay is the difference between that moment in time when the blockage expires ($last_attempt + $delay because from $last_attempt on, the user is blocked for a period of $delay) and the current time (time()) - therefore the correct formula is:
$remaining_delay = ($latest_attempt + $delay) - time();
if ($remaining_delay > 0) {
die('Access denied, you need to wait another '. $remaining_delay .' seconds');
}
Suppose the target time is 4.30 pm and the current time is 3.25 pm , how will i calculate the minutes remaining to reach the target time ? I need the result in minutes.
session_start();
$m=30;
//unset($_SESSION['starttime']);
if(!$_SESSION['starttime']){
$_SESSION['starttime']=date('Y-m-d h:i:s');
}
$stime=strtotime($_SESSION['starttime']);
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that
echo round(abs($ttime-$stime)/60);
Krishnik
A quick calculation of the difference between two times can be done like this:
$start = strtotime("4:30");
$stop = strtotime("6:30");
$diff = ($stop - $start); //Diff in seconds
echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc.
Edit*
Might as well add the answer to your problem too:
$start = strtotime("3:25");
$stop = strtotime("4:30");
$diff = ($stop - $start);
echo $diff/60; //Echoes 65 min
Oh and one more edit:) If the times are diffent dates, like start is 23:45 one day and end is 0:30 the next you need to add a date too to the strtotime.
i have the flowing code
$LastModified = filemtime($somefile) ;
i want to add ten minute to last modified time and compare with current time then if $LastModified+ 10 minute is equal to current time delete the file . how can i do that ?! i'm little confusing with unix time stamp .
Since the UNIX timestamp is expressed in "seconds since 1970", you just add five minutes in seconds:
$LastModPlusFiveMinutes = $lastModified + (60 * 5);
Or, maybe more readable:
$LastModPlusFiveMinutes = strtotime("+5 minutes", $lastModified);
The unix timestamp is the number of seconds that have passed since Jan 1st, 1970.
Therefore to add 10 minutes you need to add 600 (seconds). To get the current time call time().
e.g.
$LastModified = filemtime($somefile);
if ($LastModified+600 <= time())
{
// delete the file
}
(Note that you said "if $LastModified+ 10 minute is equal to current time delete the file" - I presume you actually meant equal to or less than, otherwise replace <= with == above).
You wrote 5 minutes in the topic and 10 minutes in the context.
Anyway here is the code
$diff = time() - $LastModified ;
if( $diff >= 10 * 60 ){ // don't use ==, you may not find the right second..
//action
}
$LastModified = filemtime($somefile);
$now = time();
if(strtotime($LastModified.'+10 minutes') >= $now){
// delete the file.
}
That should do it.