PHP expiration date

Background information

I am creating a donation / purchase system for garrys mod. when a user donates via paypal i have php code that processes the ipn and enters data into the database, it also generates a serial key and email it to that user who can then put this into play when the user enters serial play it checks the database to see if the key is valid and then executes the appropriate lua code. The system still works great, however donation packages last for a specific time period of 30 days, 90 days, 180 days and 365 days.

What I want to achieve is when the user enters the key into the game, I will have lua code that stores the players' Steam IDs as well as the day they bought it out and how long the package will last. I will set up a cron job to run a script that checks the database, if the key is in use, if the key has been used, it will check if, for example, 30 days have passed since the redemption of the key, if it is below 30 days do nothing, if within After 30 days, it will change the value in the table from 0 to 1.

Question

The part where I am really trying to figure out how to store the date and check the date, initially I thought I would use the day of the year, but then the question comes out about changing the year from 365 to 0 if someone buys it in December for 90 days, then errors will occur. So how can I do this while reliably accounting for the change of year?

Thanks to everyone who posted the answers and very quickly I think I have enough to work with it now, thanks again for your time

+3


source to share


3 answers


Well, I don't know your database structure, but I will say something like this:

user_table
-----------------------------------------
user_id   | date_of_redeem | subscription
-----------------------------------------
user1     | 2014-02-01     | 90
user2     | 2014-05-18     | 365
user3     | 2014-07-22     | 180

      

So, using only SQL statements, you can use something like DATE_SUB. Compare date_of_redeem with today's date minus the subscription amount. If date_of_redeem is higher you will get the result, otherwise it will return an empty result set.

SELECT *
FROM `user_table`
WHERE `date_of_redeem` > DATE_SUB(NOW(), INTERVAL `subscription` DAY) AND `user_id` = 'desired_user_id'

      



Edit: The SQL script for the above query: http://sqlfiddle.com/#!2/f95ea/11 (just change user3

to any of the user ids to check, return any results.

if you want to get a table with all the expiration dates of the subscription, you can do something like this:

SELECT `user_id` AS `User`, (DATE_ADD(`date_of_redeem`, INTERVAL `subscription` DAY)) AS `Subscription Ends`
FROM `user_table`

      

SQL Fiddle for this: http://sqlfiddle.com/#!2/f95ea/9

+2


source


First, you will provide a date when the package will expire. (This can be done with the DateTime class PHP if needed). Then you can just compare it withtime()



$date = new DateTime('2014-01-01'); //purchase date
$date->add(new DateInterval('P30D')); //in this case for 30 days
if(time() < $date->getTimestamp())
{
    //expired
}

      

+2


source


Use a Unix timestamp . It stores the current date as a number. Specifically, the number of seconds that have passed since the Unix era.

When they get the code done, save the current Unix timestamp. When you check, subtract the stored Unix timestamp from the current Unix timestamp. If it is within 24 * # days Valid, deactivate the code.

Save it

<?php
$date = time();
?>

      

Check

<?php
$validNumDays = 30;
$validNumSeconds = $validNumDays * 60 * 60 * 24;
$dbtime = GetStoredTimestamp();
$diff = time() - $dbtime;

if($diff > $validNumSeconds) { Deactivate(); }
?>

      

+1


source







All Articles