PHP - Check if hash has expired - php

I want to check if a hash I've stored in my database is expired, i.e older than 30 minutes.
This is my check
$db_timestamp = strtotime($hash->created_at);
$cur_timestamp = strtotime(date('Y-m-d H:i:s'));
if (($cur_timestamp - $db_timestamp) > ???) {
// hash has expired
}

Timestamps are numbers of seconds, so you want to see if the age of the hash (in seconds) is greater than the number of seconds in 30 minutes.
Figure out how many seconds there are in 30 minutes:
$thirtymins = 60 * 30;
then use that string:
if (($cur_timestamp - $db_timestamp) > $thirtymins) {
// hash has expired
}
You can also cleanup the code by doing the following:
$db_timestamp = strtotime($hash->created_at);
if ((time() - $db_timestamp) > (30 * 60)) {
// hash has expired
}

Related

Check is 10 minutes passed

I need to check if a the time between session_start and current time exceeded 10 minutes. I've tried this:
$session_duration_max = 10; // 10 min
$current_time = time();
if ((time() - $_SESSION['session_start']) > $session_duration_max ) {
// session expired
}
// elsewhere I set session
$_SESSION['session_start'] = time();
But I keep getting 0 when I subtract time() - $_SESSION['session_start'])
What am I missing?
time() is measured in seconds. To correctly set the timeout after 10 minutes, you must multiply the timeout duration by 60 seconds.
// Session does not exist
if(!$_SESSION) {
// First Session:
$_SESSION['session_start'] = time();
}
$session_duration_max = (10 * 60); // 10 min
// Check if current time is larger than timeout
if ((time() - $_SESSION['session_start']) > $session_duration_max ) {
// session expired
// delete session and require
}
Above is a better idea of a system that would make sure timeout is honored.

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.

PHP - Show link expiration time in minutes

I want to set a link expiration date with php:
I want that when user creates a new short link on my website it should be automatically deleted on the fifth day of creation.
I am totally confused with the following code. I want to put this code on user's notification page, so that it can inform them how many minutes are remaing for the link to be expired.
<?php
$created=time();
$expire=$created + 5;
$total_minutes=$expire / 5 / 60;
echo "Link expires in $total_minutes minutes";?>
It outputs an unexpected long number.
How can I implement this code so that it can output 7200 or remaining minutes?
time() returns UNIX timestamp.
If you want human readable output, look into DateTime class in PHP: http://php.net/manual/en/class.datetime.php
Example:
<?php
$created = new DateTime('now');
$expiration_time = new DateTime('now +5minutes');
$compare = $created->diff($expiration_time);
$expires = $compare->format('%i');
echo "Your link will expire in: " . $expires . " minutes";
?>
The php function time() returns in seconds (since the Unix Epoch).
You adding "5" is just five seconds.
For five days you need to add sum of 5 * 24 * 60 *60 which is the number of seconds for five days.
In code:
$created = time();
$expires = $created + (5 * 24 * 60 * 60);
if ($expires < time()) {
echo 'File expired';
} else {
echo 'File expires in: ' . round(((time() + 1) - $created) / 60) . ' minutes';
}
Please refer to PHP: time()
<?php
$created = strtotime('June 21st 20:00 2015'); // time when link is created
$expire = $created + 432000; // 432000 = 5 days in seconds
$seconds_until_expiration = $expire - time();
$minutes_until_expiration = round($seconds_until_expiration / 60); // convert to minutes
echo "Link expires in $minutes_until_expiration minutes";
?>
Note that $created shouldn't be made at script runtime, but saved somewhere, otherwise this script will always report that the link expires in 5 days.

Maths on PHP Timestamps

I have the following block of PHP:
if ($data->timestamp > (time() - 10 * 60) ) {
// data is newer than 10 mins
} else {
// data is older than 10 mins
}
That basically says if the timestamp stored in $data is older than 10 minutes.
I'm not sure how this is worked out exactly, and I've read about the time function here: http://php.net/manual/en/function.time.php
I want to edit it to become every hour instead of 10 minutes.
Can anyone share some advice/resources? e.g. how I would change it to become 1 hour and how exactly this is worked out.
This code line checks if the timestamp saved in the $data is NOT older than 10 minutes.
the time() function gives UNIX tiemstamp of the machine in seconds. If you want to change it to an hour its 60 minutes in 60 seconds 60*60. Your code will be like this:
if ($data->timestamp > (time() - 60*60) )
it is worked out starting from the right to left in seconds. One hour would be
if ($data->timestamp > (time() - 60 * 60);
Formatt
time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
You are multiplying it as you work back 60 multiplied by 60 minutes etc etc

add five minute to filemtime function (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.

Categories