Reputation: 2425
I am using PHP Codeigniter
I have tables users which has a field balance datetype is ENUM('Yes','No'). I also have a field called reset_date which stores the date when the user's balance is set to yes. So i want to count 30 days and after 30 days want to reset that user balance again to No. So for each user if his balance user is set to yes then i want to count 30 days and reset balance after 30 days.
i am storing the current date when Balance is set to yes by :
if($this->input->post('balance') === "Yes")
{
$key= $this->session->userdata('key');
$this->reseller_m->deduct($key);/// pass youe id here
$this->load->helper('date');
$this->db->set('reset_date',unix_to_human(now()));
$this->db->where('id',$id);
$this->db->update('users', $data);
}
Upvotes: 0
Views: 250
Reputation: 5372
Alternative solution:
You can do this without a cron job: just use on column called balance_timestamp which is a timestamp of when the field was set to yes. if it's not set (null
), it means no.
Now, instead of SELECT .. WHERE balance="Yes"
, just call SELECT .. WHERE balance_timestamp>NOW()-30
which returns all items which were set to yes in the last 30 days
Alternatively, you could call your field balance_validuntil
and set it to NOW()+30
. This makes it easier to select: SELECT .. WHERE balance_validuntil>=NOW()
This is the code for writing.
if($this->input->post('balance') === "Yes")
{
$key= $this->session->userdata('key');
$this->reseller_m->deduct($key);/// pass youe id here
$this->load->helper('date');
$this->db->set('balance_validuntil',unix_to_human(now()+30));
$this->db->where('id',$id);
$this->db->update('users', $data);
}
}
For selecting:
$this->db->select('*');
$this->db->where('balance_validuntil >=', unix_to_human(now()));
Upvotes: 1