5
\$\begingroup\$

The following script is being run with a cron. It runs once a minute to see if there is a reminder that needs to be sent.

Should I make this run once every 30 minutes? The choices to send a reminder are, up to 12 hours:

  • 30 minutes
  • 60 minutes
  • 90 minutes
  • 2 hours

Could this script be optimized/written differently?

 <?php
    require_once ("connect.php");
    require_once ("function.php");
    $sql = "Select
      signings.signstart,
      signings.starttime,
      remindertime.rtime,
      signings.id,
      signings.fname,
      signings.lname,
      signings.street1,
      signings.street2,
      signings.city,
      signings.zip,
      signings.hphone,
      signings.cphone,
      signings.email,
      signings.coworkphone,
      signings.coname,
      signings.cocellphone,
      profile.timez,
      profile.email As email1,
      profile.cell,
      profile.textmsg,
      profile.emailORmessage,
      profile.emailmsg,
      profile.servid
    From
      signings 
      Inner Join
      profile On signings.pid = profile.id 
      Inner Join
      remindertime On remindertime.remiderid = profile.remiderid
    Where
      signings.signstart = CurDate() And
      signings.done = 0";
    foreach ($db->query($sql) as $stm) {

        $newsub = $stm['rtime']; // the time that needs to subtracted from the scheduled date this comes from the remindertime table 30 minutes 1 hour 2 hour ..etc 

        $datetrue = $stm['signstart'] . ' ' . $stm['starttime']; //The schedule appointment time

        $dateTime = new DateTime($datetrue, new DateTimeZone($stm['timez'])); //DateTimeZone is the timezone that the User is working in America/Los_Angeles or America/New York ...etc
        $senddate1 = $dateTime->format("Y-m-d H:i"); //the date in the selected timezone
        $date = date_create($senddate1);
        $date1 = date_sub($date, date_interval_create_from_date_string($newsub)); //subtract the amount of time before the appontment time you  want the reminder sent
        $senddate = date_format($date1, 'Y-m-d H:i'); //the datetime to send the reminder


        if ($senddate == date('Y-m-d H:i')) { // if the date is equeal to the date and time right now then send the reminder

            //get all the customers data to put in the message
            echo $pid = $stm['pid']; // the agents id number


            $emailORmessage = $stm["emailORmessage"];

            if ($emailORmessage == 1) { //if 1 then send cell phone message
                $textmsg = $stm["textmsg"]; // if textmsg is 1 yes send message
                if ($textmsg == 1) {
                    $servid = $stm["servid"];
                    $domname = cellserver($servid, $db);
                    $order = array(
                        "(",
                        ")",
                        " ",
                        "-");
                    $replace = '';
                    $cell = str_replace($order, $replace, $stm["cell"]);
                    $emailaddress = $cell . $domname;

                    //Now send message
                    $signingid = $stm['id'];
                    $to = $emailaddress;
                    $subject = 'Reminder';
                    $message = cellmessage($signingid, $db, $datetrue);
                    $headers = 'From: rdkurth@gmail.com' . "\r\n" . 'Reply-To: rdkurth@gmail.com' .
                        "\r\n" . 'X-Mailer: PHP/' . phpversion();

                    mail($to, $subject, $message, $headers);
                } //end if textmsg = 1


            } else { //send email
                $emailmsg = $stm["emailmsg"]; // if emailmsg is 1 yes send email

                if ($emailmsg == 1) {
                    $email1 = $stm["email1"];

                    //Send the email


                    $to = $email1;
                    $subject = 'Appointment Reminder';
                    $message = emailmessage($signingid, $db, $datetrue);
                    $headers = 'From: rdkurth@gmail.com' . "\r\n" . 'Reply-To: rdkurth@gmail.com' .
                        "\r\n" . 'X-Mailer: PHP/' . phpversion();

                    mail($to, $subject, $message, $headers);
                } //end if emailmsg = 1

            }
        } //end of if senddate =
    } //end foreach


    ?>
\$\endgroup\$
1
\$\begingroup\$

I don't know how important everything is, but ideally, you don't need to check everything every minute, and even if you do, you could 'queue' up a cron job from another cron.

For instance

  • cron-1 runs every week and moves reminders that need to run into a reminder file.
  • cron-2 runs once a day and moves reminders that need to run into a reminder file for cron 3.
  • cron-3 runs once an hour and moves reminders into a file for cron 4.
  • cron 4 runs once a 'minute' as you have and runs from file 4.

This would be 'more' efficient, but what I would do is just queue everything up for the hour, and use JavaScript as a timer.

IE Cron1->Reminder for 6/16->move to file->cron2->reminder for 6/19->move to file->cron 3->reminder for 6:30pm->que for web page->

javascript_timer_function(x,y, z)
{
    [get the hour and figure out how many more milliseconds]
    window.setInterval("alert('.z.')", y);
} 
\$\endgroup\$
1
  • \$\begingroup\$ I have changed it to run every 30 min because the smallest time to send a reminder before the appointment in 30 min \$\endgroup\$ Jun 19 '14 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.