Foros del Web » Programando para Internet » PHP »

Ayudita con fecha programada

Estas en el tema de Ayudita con fecha programada en el foro de PHP en Foros del Web. Hola a todos El codigo es para enviar mensaje recordatorio. Fuente Indico la fecha en que llega el mensaje pero siguen llegando dias antes de ...
  #1 (permalink)  
Antiguo 04/01/2016, 12:01
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
Ayudita con fecha programada

Hola a todos
El codigo es para enviar mensaje recordatorio.
Fuente

Indico la fecha en que llega el mensaje
pero siguen llegando dias antes de la fecha


Ejemplo estamos en el dia 4 (hoy) :

Fecha dia 8 en adelante aun no envia muy bien.
(BIEN POR QUE AUN NO ENVIA).
target_date ----- date_added ----- action_step1_date_sent --
2016-01-08 ------ 2016-01-01 09:00:00 -- 0000-00-00 00:00:00


Fecha dia 7 ó dia 6,5,4,3 si envia todo
los mensajes que tengan dichas fechas.
target_date ----- date_added ----- action_step1_date_sent --
2016-01-07 ------ 2016-01-01 09:00:00 -- 2016-01-04 09:07:16


Fecha dia 8 con anio 2015 si envia todo
los mensajes que tengan dichas fechas.
target_date ----- date_added ----- action_step1_date_sent --
2016-01-08 ------ 2015-11-01 09:00:00 -- 2016-01-04 09:07:16


Como me podrian brindar su ayuda con esto.

target_date (FECHA PROGRAMADA "Recordatorio")
date_added (FECHA CREADA del mesnaje)
action_step1_date_sent (FECHA DE ENVIO del mensaje)

NOTA: una vez que envia el mensaje ya no se vuelve
enviar el mensaje a los email hasta ahi todo bien.
Se hace un update creando una fecha en el DB para
que no se envie. disculpen la redundancia.

en cron esta programado para cada diez minutos
eso se puede cambiar no hay problema.


Código PHP:
Ver original
  1. <?php
  2. date_default_timezone_set('America/New_York');
  3. // How long in between each action step reminder.
  4. // define(INTERVAL, 86400*2); // 2 days, in seconds.
  5. // Interval is no longer constant -- it's now a function
  6. // of the target date. - Joel 2014/Jun/08
  7. // Change these to match your actual address(es):
  8. define(SENT_FROM_ADDR, '[email protected]');
  9. define(REPLY_TO_ADDR, '[email protected]');
  10. // Get command line argument; default to step 1.
  11. //
  12. // (This is probably overkill for a learning exercise, but I want to
  13. //  demonstrate the importance of validating input here, because we here
  14. //  use the input directly in our query, as part two column names we
  15. //  select; this is super-hazardous, so extra care is essential.)
  16. //
  17. $valid  = array(1, 2, 3);
  18. $which_step = count($argv) > 1
  19.            && $argv[1] == (integer) $argv[1]       // Is it a number?
  20.            && in_array((integer) $argv[1], $valid) // Is it 1, 2 or 3?
  21.             ? (integer) $argv[1] : 1;              // Legit? Or default to 1.
  22. // Translate the integer number (1, 2 or 3) into a column name.
  23. $step_col_name = 'action_step' . $which_step;
  24. $step_date_col = $step_col_name . '_date_sent';
  25. // Query to select only the action steps matching the
  26. // given number, that haven't already been sent out.
  27. // NOTE: the date math can probably be done differently,
  28. //       but for now, we stick to UNIX epochs.
  29. $select_query = "SELECT email_addr,$step_col_name,date_added,Names,"
  30.               . "       (UNIX_TIMESTAMP(target_date) - "
  31.               . "        UNIX_TIMESTAMP(date_added))/(4*86400) AS interval_size"
  32.               . " FROM client_contact"
  33.               . " WHERE $step_date_col=0"
  34.               . " HAVING UNIX_TIMESTAMP(NOW()) >= "
  35.               . "        UNIX_TIMESTAMP(date_added) + interval_size*$which_step*86400";
  36. // Select matching rows.
  37. $link = new mysqli('localhost', 'contactor', '7sdfa3w8', 'test');
  38. $select_res = $link->query($select_query);
  39. // Iterate over rows, send email, and update the sent
  40. // date column for the rows corresponding to the emails sent.
  41. while ($row = $select_res->fetch_array()) {
  42.     list($addr, $step_col_value, $date_added, $Names) = $row;
  43.     // Send the email, and print out that this happened. (Another approach
  44.     // instead of printing,  would be to blind carbon-copy yourself.)
  45.     $headers = 'From: ' . SENT_FROM_ADDR . "\r\n"
  46.              . 'Reply-To: ' . REPLY_TO_ADDR . "\r\n";
  47.     mail($addr,                                           // Mail-to address.
  48.          "Action Step Reminder - ($Names) Step $which_step",       // Subject.
  49.          "Your action step for today: $step_col_value.",  // Body.
  50.          $headers);                                       // Additional headers.
  51.     echo "Sent message to `$addr`, for action step `$which_step`,",
  52.          " which was `$step_col_value`\n";
  53.     // Record the timestamp that it was sent, so it won't get selected next time.
  54.     $update_time = date('Y-m-d H:i:s');
  55.     $update_query = "UPDATE client_contact SET $step_date_col=? "
  56.                   . " WHERE email_addr=? AND $step_col_name=? AND date_added=?";
  57.     $stmt = $link->prepare($update_query);
  58.     $stmt->bind_param('ssss', $update_time, $addr, $step_col_value, $date_added);
  59.     $update_res = $stmt->execute();
  60.     // WUT? (This is for debugging; get rid of this, for production.)
  61.     if (!$update_res) {
  62.         echo "UPDATE FAILED:\n\n";
  63.         var_dump($update_query);
  64.         var_dump($update_time, $addr, $step_col_value, $date_added);
  65.         exit;
  66.     }
  67. }
  68. ?>

Etiquetas: mysql+php
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:34.