PDA

View Full Version : PHP time



nekng
07-24-2006, 06:46 PM
How could i display diffrent lines of text based on the time, for example i want to make it so when my server time is 2:00am it will display that we are open untill the server time is 3:00am. Then it will go back to saying that we are closed, then again at 9:00pm untill 10:00pm it will say that we are open.

Twey
07-24-2006, 07:10 PM
<?php $hr = date('G'); ?>
<?php if($hr == 2 || $hr == 9) { ?>

<p>We're open.</p>

<?php } else { ?>

<p>We're closed.</p>

<?php } ?>

InNeedofHelp
07-24-2006, 07:53 PM
Is there anyway to do something by the hour? Like every hour function update() is called, and it updates every users information.

blm126
07-24-2006, 08:34 PM
Cron Jobs (http://www.google.com/search?q=cron+jobs&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&hl=en-US&client=firefox-a) are probably better at this then PHP.However yo can do it with PHP like this.

<?php
function update(){
//this will be called once per hour
return true;
}
$current_time = mktime();
$last_updated = file_get_contents("last_updated.txt");
$diff = $current_time - $last_updated;
if($diff >= 3600){
//it has been an hour since update
update();
if($fp = @fopen("last_updated.txt","w")) {
//Write time to file
$contents = fwrite($fp,$current_time);
fclose($fp);
}
else{
die("Could not write to file")
}
}
?>

InNeedofHelp
07-24-2006, 09:18 PM
Aha, brilliant. Thank you.

Twey
07-24-2006, 09:36 PM
Hmm... but if nobody visits it for an hour, that hour's update will be neglected. I'd keep current. This can also be simplified drastically:
define (http://www.php.net/define)('DATA_FILE', 'update_tracker.dat');
$c = floor (http://www.php.net/floor)(date (http://www.php.net/date)('U') / 60 / 60 / 24);
$fc = file (http://www.php.net/file)(DATA_FILE) or die (http://www.php.net/die)('Failed to open file for reading.');
$l = $fc[0];

while($l++ < $c)
update();

$f = fopen (http://www.php.net/fopen)(DATA_FILE, 'w') or die (http://www.php.net/die)('Failed to open file for writing.');
fwrite (http://www.php.net/fwrite)($f, $c);
fclose (http://www.php.net/fclose)($f);blm126: Single quotes are significantly faster to parse than double, and so should be used wherever double quotes are not needed.

InNeedofHelp
07-24-2006, 09:39 PM
Twey, would you mind explaining your code please? It's pretty complicated to me, i dont know how to work with files yet.

Twey
07-24-2006, 09:49 PM
I've linked to the relevant documentation. Does that help?

InNeedofHelp
07-24-2006, 09:50 PM
Great. Thanks. :D

nekng
07-24-2006, 10:35 PM
<?php $hr = date('G'); ?>
<?php if($hr == 2 || $hr == 9) { ?>

<p>We're open.</p>

<?php } else { ?>

<p>We're closed.</p>

<?php } ?>
it works, thank you!

blm126
07-24-2006, 10:42 PM
Twey: Thanks for the tip I've never heard that before. Also I might have done my math wrong, but won't your code only run update() once a day. I think it should be this.


<?php
define('DATA_FILE', 'update_tracker.dat');
$c = floor(date('U') / 60 / 60);
//Removed the extra /24 above
$fc = file(DATA_FILE) or die('Failed to open file for reading.');
$l = $fc[0];

while($l++ < $c)
update();

$f = fopen(DATA_FILE, 'w') or die('Failed to open file for writing.');
fwrite($f, $c);
fclose($f);
?>

InNeedofHelp
07-24-2006, 11:05 PM
I understand the functions going on in that script, but can one of you please explain to me the theory behind the script? Like, the while loop in particular.

blm126
07-24-2006, 11:41 PM
The file update_tracker.dat is used to keep track of when update() was last run.When the script loads it compares the current time($c) to the time held in update_tracker.dat($l).The while loop is used to run update() once for every hour that has passed.Then the current time is saved to update_tracker.dat

Twey
07-24-2006, 11:45 PM
Also I might have done my math wrong, but won't your code only run update() once a day.Yep, you're right. I was never good at maths. :p

InNeedofHelp
07-25-2006, 12:32 AM
Dang this is confusing. :o
I get everything except, lets say someone accesses the page and that script is run, then 2 hours later the script is run again, that would write to the file something like 2:00pm, 4:00pm, but it would leave out 3:00pm so wouldn't the update not happen at 3?

Twey
07-25-2006, 01:03 AM
That's what my code is for.

Breaking them both down into English, on each access, blm126's code willopen the file and check if it's been run this hour; if so, run the function; update the file.Mine willopen the file and check when it was last run; for each hour in which it wasn't run, call the function once; update the file.The updates won't necessarily happen on time, but they will all happen.

InNeedofHelp
07-25-2006, 02:18 AM
Ooooohhhh.
Okay, now i get it. I'm not good with date functions nor file functions. :p
Thanks. :D