echo-tech Posted July 16, 2013 Share Posted July 16, 2013 Hi, I have a question... I have created a PHP system where you can purchase various membership systems (e.g Bronze, Silver and Gold). When the membership is purchased it updates the following: Sets "package_bronze" to "1" Inserts date and time + 1 month into "bronze_exp_date" (e.g today is 16/07/2013, so it'll set the exp date to 16/08/2013)... I want to setup a cronjob so that every 5 minutes it'll run a PHP script to tell the database whether or not the users membership should have been set from "1" back to "0". I need some help with writing that code... I'm assuming it would be something like: If a certain user's expiry date has passed then set the certain package variable date to 0 ... Could anyone please help me with the mysql query for it and the php "if" statements? Regards, Echo-Tech Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/ Share on other sites More sharing options...
Solution fastsol Posted July 16, 2013 Solution Share Posted July 16, 2013 $today = date("d/m/Y"); mysql_query("UPDATE `table` SET `column` = 0 WHERE `expire_date` < $today"); I did a little assuming on the $today and expire_date, this may not work correctly depending on the format you have the date stored in the db. If it's in a DATE format it might work right that way, otherwise I suggest reading up on the mysql date formats and queries for such things. Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1440952 Share on other sites More sharing options...
AbraCadaver Posted July 16, 2013 Share Posted July 16, 2013 I can't really see this being useful. Why not just wait until the user wants to login and check the expiry? I can only see this as being useful if another system is accessing the data, but then it can check the expiry as well if you control that system. Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1440966 Share on other sites More sharing options...
echo-tech Posted July 17, 2013 Author Share Posted July 17, 2013 Thanks fastsol I see how that works now... I can't really see this being useful. Why not just wait until the user wants to login and check the expiry?I can only see this as being useful if another system is accessing the data, but then it can check the expiry as well if you control that system.Just a thought. I suppose, but is there absolutely no way that the user can bypass the expiry date check? I want the system to be fast, if the user logs in and has 3 expiry dates to each package (for example) then it's going to take a while for the user to get logged in, not too long but it'll be long enough to be annoying after each check has to be complete... Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1441042 Share on other sites More sharing options...
fastsol Posted July 17, 2013 Share Posted July 17, 2013 It should seriously only take maybe an extra 5th of a second for the server to run the queries, I wouldn't worry about it. Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1441051 Share on other sites More sharing options...
echo-tech Posted July 17, 2013 Author Share Posted July 17, 2013 Okay thanks, So would you recommend not using this cronjob unless in the future I maybe wanted to add a feature to email users deals if they don't have the membership package, or maybe a reminder that it's expiring etc.. ? Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1441052 Share on other sites More sharing options...
AbraCadaver Posted July 17, 2013 Share Posted July 17, 2013 Thats up to you. I would only run it if I was sending reminders or deals. Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1441071 Share on other sites More sharing options...
kicken Posted July 17, 2013 Share Posted July 17, 2013 Even if you add a separate email task you still don't really need to add a cron job just to check the expiry date. You also don't really need separate 1/0 and date fields for each level. Just allow the date field to be null. Then you could check these conditions if bronze_exp_date = null then they never had a subscription if bronze_exp_date is a date, but expired then they had a subscription that expired if bronze_exp_date is a date and not expired then they have a current subscription. You can use a CASE statement to query pseudo-columns for this information: SELECT CASE WHEN bronze_exp_date IS NULL THEN 1 ELSE 0 END as hasHadSubscription , CASE WHEN bronze_exp_date < CURDATE() THEN 1 ELSE 0 END as hasSubscriptionExpired FROM blah For convenience you could wrap that up into a VIEW and use that view anywhere you want to query such information. Quote Link to comment https://forums.phpfreaks.com/topic/280208-php-membership-cronjob/#findComment-1441102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.