Time Based Events In PHP

  • Thread starter Thread starter Spock
  • 7 comments
  • 1,418 views

Spock

Not In My Name
Premium
Messages
3,341
Once again i am stuck with a predicament in php.
right now i am working on a personal project called AudioBase, which i plan to store my music cd's information on (year relesed, genre, artist, ect....). its mostly working right now. but the problem i am facing is how can i have php do something on a daily bases. i want to know because i have GD2 generating my logo with the date on it everytime a user accesses any page, which is a waste of resources, but i want it to generate a new one only if its past midnight, not all the time.

thanks in advance, but dont give me exact source please. just the genreal idea. i like to write all of my code on my own
 
I don't program in PHP, but here is a general idea....

1) Store date/time of last image generation in a local file.
2) When program is run, compare day stored to curent time and see if it is the next day (means image needs to be regenerated). If so, generate the image and overwrite the last date stored in the file.
3) You probably want to consider the case in which there is no such file present (for some reason) and, if so, generate the image and create the file.
 
Skip0110 is on the right lines. PHP only executes when a page is being sent to the browser, so there is no real way to make something happen in the background, without some form of prompt from a user.

So, you would need to generate the image, and store it somewhere, either as a file on the server's filesystem, or in a database. If you store the file in the filesystem, you would just name it according to its creation date, so "2005-02-12.jpg". You could then do an if(file_exists(date(Y-m-d).".jpg")) test. If the file does exist, you show it, if not, you generate a new file and show that instead.

If you were storing the file in a database, you would also need to store the creation date. You would then have a look in the database to see if you had a record created today.
 
Yeap. I created a dynamic forum sig image that pulls my most recently played track from my Audioscrobbler profile. But, the Audioscrobbler blokes only want the data accessed every 3 minutes or so. It works by first creating an image on the server called cache.png. Then when the script is called next, it'll check the file creation time against the current time and if it's been over 3 minutes it'll regenerate a new image, otherwise it'll just display cache.png.

You can see the end result here if you want.
 
Why not use PHP. So what if the image is only generated whenever a user visits the page. There's no problem with this, especially since you dont HAVE to generate a new image. You can just display the already-stored one.

I suggest reading up on the function filectime(). This function retrieves the file's creation date, and returns it as a unix timestamp. Using this in conjunction with the function date() can do what you wish. Even if it is only whenever the user visits the page.

PHP:
if (date("j",filectime("image.jpg")) != date("j")) {
    //image generate
} else {
    //image load
}

What this does is check the files date, and if the day (number) is not equal to the current day (number, as in its the next day) it will execute the image generation code. If not, it will execute the image loading code, to load the saved image.

It's rather simple, but it should get the job done.. If someone visits the page one day, and then comes back the next (after 12:00am, which will be the next day), the image will be generated..

Pretty simple. You don't have to use this code if you dont want to, though. Even though it seems the simplest (to me, atleast).

EDIT:

Also, this probably isnt recommended, and im not 100% if it would work, but you might be able to do something like so:

PHP:
<?php
while($halt != TRUE) {
    //image generate code
    
    sleep("43200");
}
?>

This would make the script start when you execute it, and then "sleep," or pause, for 12 hours. It would then loop and generate the image again.. The only thing about this is i'm not 100% sure if scripts continue runing on the server if the user closes the browser window. So if you were to do this you'd need a browser window open all the time, and i believe this would really be bad for the server... I presonally recommend my first idea.
 
For those of your who know what Aardvark Topsites PHP 5 is it updates all the stats it has collected over the day and moves everything back at the end of the day, my ATS is running with a CronJob rather than a date check system which the released version has. It reduced the amount of work the server had to do when the request was made which granted is only once a day. It actually uses less code as well, but not everyone has access to make CronJobs or the Windows equivelent which is why it wasn't used.
 

Latest Posts

Back