skip to Main Content

just want to ask information.
can I update data automatically in MYSQL using codeigniter 4?
for example: if I have a table named table1 and it has fields number(int),dateStart(datetime). For starters, I enter a value into the number field, which is 0. Can I update the data in the number field to 1 if today’s date has passed the date in the dateStart field?

and if can, how to do it, is it in the model? controller? or is there something new I should know?

2

Answers


  1. So, you are storing some value. If at the time of saving dateTime has already passed, then you can reject it with a validator.

    Otherwise, if it was today and you want it to be updated to the other value at midnight, then you are looking for a cron job.

    Login or Signup to reply.
  2. The following snippet should work however it is recommended a cron job as the first answer.

    <?php
    
    $date = new DateTime("now");
    
    $dateformat = $date->format('Y-m-d');
    
    $this->db->set('number', 1);
    $this->db->where('number', 0);
    $this->db->where('DATE(dateStart)<', $dateformat);
    $this->db->update('table1');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search