I need to find the difference between 2 dates. In VB there's a function for this called "DateDiff" but in php i don't know if any one knows how to achieve this then please let me know.
Or calculating the hours will also do the same thing. I want to get 168 hours difference using time. Can this be done ?
The easiest way is to use UNIX timestamps which are in seconds. Define a variable for the number of seconds in an hour and get the UNIX timestamp of both the dates:
if ($date2 - $date1 > $seconds_hour * 168)
echo ('the difference between these two dates is greater than 168 hours');
else
echo ('the diefference between these dates is less than 168 hours');
Thanks for the code. But it has some problems for me. For eg. i am using a mysql database. Now i have to first store a time stamp. then when comparing i have to first retrieve the time stamp then get the current time stamp then match them both then if its more than 7 days i have to run another query to update the mysql database.
Now i was looking if all the above can be just done with 1 single sql query...is it possible ? If i have stored the time in db then just one sql query should update all the results if they are more than 7 days old.
Originally posted by visualAd You could probably do it with an update query: whereby you only update a record if fields match a certain criteria.
I could probably provide you with some SQL but you'll need to give me more information.
MySql has a group of Date/Time functions like PHP. These are similar to the ones you find in VB.
Hello!
Please let me know what more information you need. Also i am making an auction type system in here. A listing has to expire in 7 days. I hope this helps!
Okay now one very important security issue + load issue related question. Suppose if i have 100 listings waiting for be updated...
now mi update the listings when anyone loads any php page on my website..means if any page is loaded then a php file named "update.php" is included and is executed to update the listings.
So this means someone is here and he loaded the main index.php page now the update.php file is included and it also starts loading...it updates 100 listings as their 7 days are over. Now i want to send email to the 100 listers who listed telling them that their listing has expired will this put any load ? Or will the page take a lot of time to achieve this process or is there any better option available ?
There are two things you need to avoid here. Firstly you don't want to be executing this code with every request made to a page. Secondly you don't want to hijack an innocent users request for a page and use it to send out 100's of e-mails. Last - this method of marking listings as expired is probably not the best approach to take.
The way I would do it is to check whether the lisiting is still valid every time the listing is viewed by someone. This is done easily with a select query and there is no need to use a field to mark the listing as expired.
I would treat sending e-mail to these users as an entirely different operation. You should carry out this operation at regular intervals - for example every 15 mins. The question you are asking is how to trigger this operation. There two ways:
This is the easiest option: If your hosting company will allow you. Set up a PHP script to run every x minutes and send out an e-mail. PHP can act as a command line interpreter and doesn't require a web server to run. The questions you need to ask is - will your web host allow you to schedule jobs? Does your web host support PHP as a CGI/CLI?
The more complex option: Now if your host is running off Windows or they don't allow you to sechdule jobs then you will have no choice but to use a users request to initiate your script. The trick is - is to ensure that you are not piggy backing off the users connection to execute the script. By using output buffers and sending a "Content-Length:" header, it is actually possible to terminate the HTTP connection with the client and have the PHP script continue running with no effect on the user who made the request.
Although this method works - you need to be careful. Once the connection has terminated, all your script's output dissappeares into oblivion - including error messages. If you do use this method I would advise that you redirect your errors to a log file, so that any errors which occur after the connection has closed can be spotted.
This is how I would do it: index.php delivers its content to the client and terminates the connection. It then checks the time. If the time is a multiple of 10 i.e 4:10, 5:40 then the script update.php is included. update.php creates a file called update.php.pid and logs the process ID of the script (if update.php.pid is already there, this means the script is already running, so it won't run again). update.php then finds all listings which are out of date and sends the user and e-mail. (you may want to consider using a field in your table to indicate if an e-mail has been sent so users don't get an e-mail every time the script executes). Once update.php has finished - it deletes the update.php.pid file.
These are the only two ways I have been able to get around this problem in the past. There may be easier ways but in my experience these methods appear to work and are safe.
Originally posted by visualAd There are two things you need to avoid here. Firstly you don't want to be executing this code with every request made to a page. Secondly you don't want to hijack an innocent users request for a page and use it to send out 100's of e-mails. Last - this method of marking listings as expired is probably not the best approach to take.
The way I would do it is to check whether the lisiting is still valid every time the listing is viewed by someone. This is done easily with a select query and there is no need to use a field to mark the listing as expired.
I would treat sending e-mail to these users as an entirely different operation. You should carry out this operation at regular intervals - for example every 15 mins. The question you are asking is how to trigger this operation. There two ways:
This is the easiest option: If your hosting company will allow you. Set up a PHP script to run every x minutes and send out an e-mail. PHP can act as a command line interpreter and doesn't require a web server to run. The questions you need to ask is - will your web host allow you to schedule jobs? Does your web host support PHP as a CGI/CLI?
The more complex option: Now if your host is running off Windows or they don't allow you to sechdule jobs then you will have no choice but to use a users request to initiate your script. The trick is - is to ensure that you are not piggy backing off the users connection to execute the script. By using output buffers and sending a "Content-Length:" header, it is actually possible to terminate the HTTP connection with the client and have the PHP script continue running with no effect on the user who made the request.
Although this method works - you need to be careful. Once the connection has terminated, all your script's output dissappeares into oblivion - including error messages. If you do use this method I would advise that you redirect your errors to a log file, so that any errors which occur after the connection has closed can be spotted.
This is how I would do it: index.php delivers its content to the client and terminates the connection. It then checks the time. If the time is a multiple of 10 i.e 4:10, 5:40 then the script update.php is included. update.php creates a file called update.php.pid and logs the process ID of the script (if update.php.pid is already there, this means the script is already running, so it won't run again). update.php then finds all listings which are out of date and sends the user and e-mail. (you may want to consider using a field in your table to indicate if an e-mail has been sent so users don't get an e-mail every time the script executes). Once update.php has finished - it deletes the update.php.pid file.
These are the only two ways I have been able to get around this problem in the past. There may be easier ways but in my experience these methods appear to work and are safe.
Hi!
Thanks for your valuable comments. But there's a little problem. Now my website is not popular yet...! It will take some time to get popular. Now what happens if the listing has to expires in 7 days and nobody visits that page in 7 days ? Then what ? The listing continues to list...and the author who listed should expect an email after 7 days but he does not gets an email.
But i like the idea of scheduling. The webhost of mine is very good and cooperative. But i am not sure weather he'll allow this or not but i'll ask anyways.
So i ask you for the detailed instructions on how to do this scheduling ? Suppose i have a file update.php which preform's 2 tasks i.e: updates the listing and sends emails after every 15 or 30 mins. So how should i go about it ?
Can i have any type of sample which demonstrates this ? even a sample with just a echo command would do. I learn better with samples.
I can give you instructions on how to do this for a Linux/Unix host. I'm not entirely sure how this would be done in Windows.
The Linux scheduler is called 'cron'. Cron maintains a file for each user on the system and you can edit it by logging into the server via SSH and typing 'crontab -e' at the shell prompt.
An entry in the crontab to run a PHP script every 15 minutes will look like this:
Code:
0-59/15 * * * * /home/user/update.php
Create your PHP script as normal and then add the following line to the top of your script and give it executable permissions. You should then be able to run the script from the command line.
PHP Code:
#!/usr/local/bin/php -f
<?php
echo ("test script\n");
?>
You will need to change the path to reflect the path of the php interpreter on the server.
Originally posted by visualAd I can give you instructions on how to do this for a Linux/Unix host. I'm not entirely sure how this would be done in Windows.
The Linux scheduler is called 'cron'. Cron maintains a file for each user on the system and you can edit it by logging into the server via SSH and typing 'crontab -e' at the shell prompt.
An entry in the crontab to run a PHP script every 15 minutes will look like this:
Code:
0-59/15 * * * * /home/user/update.php
Create your PHP script as normal and then add the following line to the top of your script and give it executable permissions. You should then be able to run the script from the command line.
PHP Code:
#!/usr/local/bin/php -f
<?php
echo ("test script\n");
?>
You will need to change the path to reflect the path of the php interpreter on the server.
Hi!
I am on Linux hosting. My host allows CORN jobs! I am using CPanel. Now here's what i see when i open the corn thing: I have attached the cpanel screen shot. Please advise me what to add in what !