Jump to content

if statement based on php time


rickphp

Recommended Posts

Hi,

 

I need a basic if function to run based on time.

 

How it will work is, the script is called.

If the script was ran in the last 30 seconds then nothing happens, else it carries out the function.

 

The last access time would have to be stored so the script could see if the current time is more than 30 seconds past the time in the file, a simple text file would be sufficient for this.

 

Can anyone advise how I'd achieve this?

 

Thanks for any help!

Link to comment
Share on other sites

Used a file like you said:

<?php

$handle = fopen('time.txt', 'r');
$old_time = fread($handle, 10);
fclose($handle);

$now_time = time();
$final_time = ($now_time - $old_time);

if($final_time > 30)      // The program you want to run goes here
{
for($i = 0; $i < 50000; $i++)
{
	$bug = 'lets put some things in it';
	$bug1 = 'lets put two things in it';
	$bug2 = 'lets put three things in it';
	$bug3 = 'lets put more things in it';
}
echo $bug.'<br />';
echo $bug1.'<br />';
echo $bug2.'<br />';
echo $bug3.'<br />';
}else{
echo 'You must wait';die;
}

$time_end = time();
$handle = fopen('time.txt', 'w');
fwrite($handle, $time_end, 10);
fclose($handle);
?>

Link to comment
Share on other sites

Thank you very much for that. One question though,

 

what is this code for?

 

for($i = 0; $i < 50000; $i++)
{

 

Its confuzzled me a bit..

 

Would it work like this?

 

<?php

$handle = fopen('time.txt', 'r');
$old_time = fread($handle, 10);
fclose($handle);

$now_time = time();
$final_time = ($now_time - $old_time);

if($final_time > 30)   {
function would go here..
}else{
echo 'You must wait';die;
}

$time_end = time();
$handle = fopen('time.txt', 'w');
fwrite($handle, $time_end, 10);
fclose($handle);
?>

 

 

 

Link to comment
Share on other sites

You can avoid the overhead of reading and writing the file by using the touch function. This function just changes the access and modification times on a file. If the file does not exist, it will be created. It is not uncommon on *nix systems to have an empty file around just to have a place to store a date-time stamp.

 

if (file_exists($checkFile)) $lastTime = filemtime($checkfile);
else  $lastTime = 0; // Never been run before

if ((time() - $lastTime) > 30) { // More than 30 seconds ago
  // Run your process here
  // The last step of the process should be ...
  touch($checkFile);
}

 

I would put some text inside the file explaining why it is there; you know, like the "This page intentionally left blank" pages in a technical document.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.