0

I am trying to add a new feature in my website, i.e I want to fix the time limit of 1 month and after that it expires that means the status of that user will change from 0 to 3.

I have many fields in my table including

  1. id
  2. status
  3. today_date.

In this, today_date stores the time and date of registration and by default the status is 0.

Is this possible using php-mysql? If possible, please help me out. Thanks in advance.

Siva
  • 1,357
  • 1
  • 16
  • 24
Bhawesh
  • 49
  • 6

3 Answers3

1

You should take another approach to achieve this.

You already have registration date in DB right, so when user enters valid credentials in the login form, compare the current date with the registration date, if that difference is more than 30 days then update the status columns with 0 and also do not allow that user in at that time.

How to calculate number of days between two dates in php:

$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));  // This will be number of days. Store it in a variable and set a condition on it.
Himanshu Upadhyay
  • 6,220
  • 1
  • 13
  • 32
  • Suppose i have more than 500 users and the registration time of every user is different then how could i do that. – Bhawesh Dec 05 '18 at 07:46
  • See, your objective is to disable the user after 30 days of registration. So if you go for the logic I explained, what will happen, when any user will try to login, the difference of days will be calculated, and if it will be more than 30 days, you can set `status=0` and also throw the user out at login page again. By doing so, the user will be disabled with the status flag and also he will not be able to login. Even though the user tries to login after 5 years, the above mentioned logic will work the same. – Himanshu Upadhyay Dec 05 '18 at 07:50
  • I just want to change the users status i.e. from status=0 to status=3 automatically after 30 days and the next steps i have already done. I just want to change its status nothing else bro and thanks for your support. Please help me more. – Bhawesh Dec 05 '18 at 08:06
  • 2
    @Bhawesh, unfortunately cron is the only option in that case. – Samir Dec 05 '18 at 08:07
  • ok bro, can you please help me out with cron job – Bhawesh Dec 05 '18 at 08:11
  • 1
    @Bhawesh, that will be a long process to explain here. You can simply google and you will find so many examples how to set Cron job. Youtube videos will be more helpful. – Himanshu Upadhyay Dec 05 '18 at 09:01
  • I lost with this answer. – lakshman Dec 05 '18 at 09:13
  • @lakshman, what do you lose with this answer? Tell me I may help you. I lost 2 reputation point due to a down vote. – Himanshu Upadhyay Dec 05 '18 at 09:35
  • How to change the status from 0 to 3 in database table automatically after 1 month using php-mysql @HimanshuUpadhyay if it's automatic then it's scheduled jobs i.e. cron jobs. I wonder the faff in understanding the question and nothing personal – lakshman Dec 05 '18 at 09:51
  • Actually @lakshman, when Bhawesh said that `"Is it possible without cron job ? I didn't want to include cron job. Give me some solution except cron job. –"` I suggested my answer as work around. – Himanshu Upadhyay Dec 05 '18 at 10:31
0

If you have a page to process the Login POST data, then build your sql query to fetch the column containing the user's time of registration. Take $time_of_reg as user's time of registration. What ever timestamp you're using, convert it to a UNIX timestamp using strtotime() function.

Now adding 30 Days to the user's time of registration $time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg)

Now use an if condition to perform an update query if the current time is greater than or equal to 30 Days + users time of registration. This way you can perform an update query to your status column, only if the user has been registered for 30 or more days.

if(time()>=$time_of_reg_plus_30)
{ //perform sql update query}

(having these values in a UNIX timestamp, makes it easier to compare)

<?//perform sql query to fetch user's time of registration, '$time_of_reg' in this case.

 $time_of_reg = strtotime($time_of_reg);  //Convert to UNIX timestamp
 $time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg); // Add 30 Days To Registration Time

if(time()>=$time_of_reg_plus_30)
{ //perform sql update query if user has been registered for 30 Days or More
}

Remember to place this in the page that handles your Login POST data, this is a good way to achieve your objective without a cron job, as the user's status is updated whenever the user attempts to Log In.

0

Cron job is the best solution for this. Think you don't want to apply cello tape on balloon.

If you would like follow the steps below:

  1. Create a PHP script & save in a file e.g. myCron.php

  2. If you have Linux server with Centos create a scheduler script with bash How to create a cron job using Bash automatically without the interactive editor?

  3. If you have cPanel go to cron jobs, set the file path & time to run.

lakshman
  • 636
  • 4
  • 17