开发者

Insert schedule for each week in a loop

开发者 https://www.devze.com 2023-03-28 16:53 出处:网络
I have set up a schedule email script using listmailpro. I need to schedule a email to be sent for each Monday of the month for the next couple of years. The example below shows what I am trying to ac

I have set up a schedule email script using listmailpro. I need to schedule a email to be sent for each Monday of the month for the next couple of years. The example below shows what I am trying to achieve. How could I write a quick PHP script that will insert the schedules for me, changing the date for each insert?

INSERT INTO `lm_schedule` (`id`, `type`, `date`, `subject`, `message`, `htmessage`, `fattach`, `list`) VALUES
('', 'm', '2011-08-15', 'Test weekly email (!date2)', 'email text', 'email body', '', '1'),
('', 'm', '2011-08-22', 'Test weekly email (!da开发者_Go百科te2)', 'email text', 'email body', '', '1'),
('', 'm', '2011-08-29', 'Test weekly email (!date2)', 'email text', 'email body', '', '1'),
('', 'm', '2011-09-05', 'Test weekly email (!date2)', 'email text', 'email body', '', '1');


Not tested but should give a rough idea:

$init = strtotime('2011-08-15');
$stop = strtotime('2013-08-15');
$step = 604800;

switch (date('w', $init)) {
    case 0:
        $correction = 1;
        break;
    case 1:
        $correction = 0;
        break;
    case 2:
        $correction = 6;
        break;
    case 3:
        $correction = 5;
        break;
    case 4:
        $correction = 4;
        break;
    case 5:
        $correction = 3;
        break;
    case 6:
        $correction = 2;
        break;
}
$init += $correction * $step;

$qry = 'INSERT INTO `lm_schedule` (`id`, `type`, `date`, `subject`, `message`, `htmessage`, `fattach`, `list`) VALUES (';
$dates = array();

for ($timestamp = $init; $timestamp < $stop; $timestamp += $step) {
    $dates[] = ' ... ' . date('Y-m-d', $timestamp) . ' ... ';
}

$qry .= implode('),(', $dates) . ');';


$date = new DateTime('Monday'); // today or next monday
$end = new DateTime('now + 10 years');

// for each monday between now and the next 10 years, insert a schedule
while ($date < $end) {
    insert_schedule($date);
    $date->modify('next Monday');
}

Or in imperative style:

$date = date_create('Monday'); // today or next monday
$end = date_create('now + 10 years');

// for each monday between now and the next 10 years, insert a schedule
while ($date < $end) {
    insert_schedule($date);
    date_modify($date, 'next Monday');
}


Try this...

<?php

  $startdate = '2011-08-15';
  $numweeks = 104; // 2 years (52 x 2)

  $oneweek = 60 * 60 * 24 * 7; // length of 1 week in seconds
  $currenttime = strtotime($startdate); // Unix timestamp of start date

  for ($i = 0; $i < $numweeks; $i++) {
    $thisdate = date('Y-m-d',$currenttime);
    mysql_query("INSERT INTO `lm_schedule` (`id`, `type`, `date`, `subject`, `message`, `htmessage`, `fattach`, `list`) VALUES ('', 'm', '$thisdate', 'Test weekly email (!date2)', 'email text', 'email body', '', '1')");
    $thisdate += $oneweek;
  }

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号