add five minute to filemtime function (php)! - php

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.

Related

PHP check if the minutes of the current time are greater than a variable value

I have to check, starting from a range of times (start and end), if the current time is included in the interval (and this is done)
// Time intervall
$range = 30;
// Time range
$start = strtotime('09:00:00');
$end = strtotime('13:00:00');
// Now
$time = time();
if($time >= $start && $time <= $end):
echo 'Inside range';
else:
echo 'Outside range';
endif;
Now I have to check if the minutes, including seconds, are greater than the variable that contains the minutes of interval.
Example: if it is 10:31:23 and my time variable is 30 minutes, I have to check if (minutes:seconds) 31:23 is greater or less than the variable, that is 30 minutes, if it is greater I have to add to the hour (10), the double the variable, that is 30 * 2 and thus obtain 11:00:00, if instead it is less I have to add only 30 to the hour, therefore 10:30:00.

calculating time remaining using php time()

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.

Check if current time is 1 minute past previously-saved time in PHP

I'm saving the time to a variable called $time, using $time = time(), and later on, I need to be able to check to see if the current time is greater than 1 minute by comparing the current time with the previously-saved time.
How can I do this in PHP?
You can save the time to database or session, but this is how you would check
$time = time();
if((time() - $time) > 60){
echo 'past 1 minute';
}

PHP Check if the current time is less than a specific time

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);

PHP Checking if timestamp is less than 30 minutes old

I'm getting a list of items from my database, each has a CURRENT_TIMESTAMP which i have changed into 'x minutes ago' with the help of timeago. So that's working fine. But the problem is i also want a "NEW" banner on items which are less than 30 minutes old. How can i take the generated timestamp (for example: 2012-07-18 21:11:12) and say if it's less than 30 minutes from the current time, then echo the "NEW" banner on that item.
Use strtotime("-30 minutes") and then see if your row's timestamp is greater than that.
Example:
<?php
if(strtotime($mysql_timestamp) > strtotime("-30 minutes")) {
$this_is_new = true;
}
?>
I'm using strtotime() twice here to get unix timestamps for your mysql date, and then again to get what the timestamp was 30 minutes ago. If the timestamp from 30 mins ago is greater than the timestamp of the mysql record, then it must have been created more than 30 minutes go.
Try something like this, using PHP's DateTime object:
$now = new DateTime();
$then = DateTime($timestamp); // "2012-07-18 21:11:12" for example
$diff = $now->diff($then);
$minutes = ($diff->format('%a') * 1440) + // total days converted to minutes
($diff->format('%h') * 60) + // hours converted to minutes
$diff->format('%i'); // minutes
if ($minutes <= 30) {
echo "NEW";
}
Edit: Mike is right, I forgot that for whatever reason, only %a actually returns the total of its type (in this case days). All the others are for displaying time formatting. I've extended the above to actually work.
You can do like this also -
$currentTime=time();
to check the last updated time is 30 minute old or not
last_updated_at < $currentTime - (60*30)

Categories