How to execute some code on a specific datetime to the second? - php

I'm building a market place with auctions. I need to end auctions preferably at the exact second they are defined to end according to their record in the mysql database.
I know about CRON jobs and I'm currently running a CRON job every minute to execute some script which checks if there are sales that should end.
However this sometimes means that an auction ends almost a minute too late, which is not acceptable. Does anybody know how to make this more accurate without putting a lot of load on the server? For example I wouldn't want to run a script every second to check if sales have ended.

Check timestamp everytime a user try to click auctions button. So even the page haven't refresh and times up, user is not able to bid your item.

Related

Execute query until timer reaches zero (PHP and MySQL)

I'm doing a module of rental videos, I've done the shopping cart but I only need to execute an update query if the user don't submit his order before within five minutes, I need a timer (I'm using PHP and MySQL) that is not reset when changing page, some like an $_SESSION, when user add a item in the cart his status on his table be "unavailable" (just to mention something) and the timer begins, and when it reaches zero do a update query and change his state to "available". What interests me most is getting the timer is maintained and not restart when changing page.
I need ideas from you, your suggestions are welcome, Thank you!
You Can store the time-stamp into Mysql table when user adds item to cart, And then create function in a separate php file that checks for time stamps that are expired and updates the status of items that are having expired times,
Now You can either use 'Crons job' to auto check for expired timers or run that function on any user interaction with system as per your needs...!!! :)
Firstly start session and store time stamp both in database and in session when user registers. Then write code that checks last request time is more than 5 mins by subtracting last time stamp with current time. If it's more than 5 mins then change state to available.
Also you can achieve this with MySQL triggers.
http://www.mysqltutorial.org/mysql-triggers.aspx

Update mysql record only once in php when multiple users calls the same function

I have a php auction bid script, the problem is when the bid get over the script makes an ajax call and update all the users bid at once. What is happening is if two users simultaneously opened the page and the auction is over the piece of code runs twice which makes the data corrupted.
Is there any way to prevent this problem ?
Add a timestamp and use the time differences to tell with the bidding is done basis on the start time and duration of the bidding war. This way if users bid after the bidding has ended you pick the closest that didn't go over or whatever your rules say to do.

PHP triggering mail functions at a set time and date

I want to check the stock level every day at a certain time (say 11pm GMT) and email a list of items that are low on stock to the admin. I have managed to get the mailing function working but I am struggling to limit the checks to once a day (right now it sends an email to the admin email every time the page loads)
Your help will be much appreciated!
I think that you just need to put your script in a separate file and run once per day with a cron job
You might want to read more about cron jobs in this post How to create cron job using PHP?
In case that impossible to run a cron job, you can save last mail time in DB or file and check timeout on page load.

Update information at specific timestamp

I'll try to explain my question the best way I can.
I'm not asking for codes, only for the best method of doing it.
I want to create a browser game and use time for upgrading stuff, building etc.
For example, to build 1 house will take 1 hour.
So I will began with saving the timestamp+(60*60) at the moment the user did his action.
My question is, how to update it the best way?
One way I thought of was to add function that check every page view of the user if it's done.
But then if he's not logged in the update wont happen.
Second way i thought about is for every page view of any user to check for every user registered. But it's not effective and there is a problem if no user is logged in.
Any suggestions?
I had my game doing it simply, without crons.
When a player wanted something that takes time, i just updated his database information with the appropriate time of ending that job (columns are just examples)
UPDATE player SET jobend = UNIX_TIMESTAMP() + (60*60*4) # ending in 4 hours
Then, every page that had an information about the remaining time, i just used something like this:
SELECT (jobend - UNIX_TIMESTAMP()) AS jobremaining FROM player
I formatted correctly the time using strftime and i displayed that to the user.
In the case the remaining time was negative, the job was done.
There was no-need for absolute counting since user was able to do something with the job when he was connected.
When the player just changed pages or doing something else i had a function where i just checked all timely events while the user was online (so to catch any negative timer), then presented with javascript any change (i posted javascript counters for every page)
Now, if you talk about updating in real-time, cron is the way but are you sure you're going to need it for a game? I asked that question myself too and the answer was not.
EDIT
If another player sees the buildings on schedule page (an hypothetical page) i am doing the same calculations; if a time just got negative for a specific player (regardless if another player see the page), i just reward him with the building (in database i make all the changes), even if he's offline. There's no harm in this, since he can't do anything anyway. The other players will just see he has a building. The key here is that i execute the required updating PHP code regardless of player's connection to the game; as long at least ONE player is logged-in i'm executing the progress function for everything.
This isn't so slow as it sounds (updating all players by using just a connected player that visits a specific page). You just have a table of 'jobs' and check timers against the current time. More like a single query of getting the negative ones.

PHP Server-side timer that executes MySQL query even if user leaves website?

I'm writing a website where users can order products, but the catch is that the products should automatically checkout (executes PHP script including MySQL queries) after 5 minutes even if the users logout/close the browser, etc. Has anyone had a problem similar to this. How should I go about coding this?
Since this is a recurring automated task, you will need to set up a cron or other task scheduler. Crudely, this is how it would work:
When user adds product to shopping cart, it goes into database with a flag indicating that its not checked out along with timestamp of addition to db/shopping cart.
If the product is checkout by the user explicitly from the browser, this flag is updated to reflect the same.
The cron wakes up every one or two mins and checks this table for timestamps of items added to shopping cart without checkout flag updated and added more than 5 mins before.
It checks out such products and updates flag.
Please note that you will still need to further tweak this flow to handle situations like what happens if the user is manually checking out while your cron is doing the same etc etc
But on a nutshell I think this is how I would proceed to tackle the issue
Store the info related to products to be checked out in database.
A cron job running every minute will cause checkout for products 5 min old.
Here is what I would do. MySQL as far as I know cannot have a timed delay. You can have a cron job that runs and executes statements. What I would do is make an extra field in the shopping cart table and set a future checkout time. Make a cron job that runs every minute on the server. Have that cron job just do a quick poll of shopping carts with checkout times less than the current time. If it finds one, run the checkout procedure on that cart and either delete it or mark it as checked-out so that it doesn't re-check it out again.
While I can't provide you with sample code as you haven't provided anything, this will at least give you a jumping off point.
Surely a person wants a similar experience to visiting a store?
Like picking up things and wandering around.
Perhaps that person has decided to leave the basket at the counter? By all means have a product on offer for a time period and inform them that the time is up (or needs to be bought withing X minutes)
So why the auto checkout stuff?
One assumes that you do not want a bad reputation over a person deciding not to buy something and you are taking the cash? Perhaps that persons internet connection has died/laptop ran out of power/...?
Just please let them have to go through a checkout and it will paid better dividends than being automatic sold stuff they might not want....
I really think that 5 minutes is to small duration for this kind of verification, but you should have a cron job which runs a php script every 5-10 minutes which verify the last date of users activities and the last (unfinished) orders. If date is smallest then actual date time() minus 5 minutes, then you should delete the entries of that user from db.
The verification should be made from orders table, and not for every existing user.
If the user is leaving the page, I suppose he requested the page (it was not automatically executed, like a cronjob).
The normal behaviour of php is to execute the script completelly, even when the usr leaves during this.
Just an idle though - Is this shopping experience is for one off products (like antiques) or run of the mill things (like groceries)?
If the latter rather than the former, why bother? Surely a good store could replenish and therefore sell that particular product at a given price regardless. If the former an auction is required.

Categories