Php 在循环中插入每周的计划

Php 在循环中插入每周的计划,php,mysql,Php,Mysql,我已经使用listmailpro设置了一个时间表电子邮件脚本。在接下来的几年里,我需要为每个月的周一发送一封电子邮件。下面的例子显示了我试图实现的目标。如何编写一个快速的PHP脚本,为我插入计划,更改每次插入的日期 INSERT INTO `lm_schedule` (`id`, `type`, `date`, `subject`, `message`, `htmessage`, `fattach`, `list`) VALUES ('', 'm', '2011-08-15', 'Test we

我已经使用listmailpro设置了一个时间表电子邮件脚本。在接下来的几年里,我需要为每个月的周一发送一封电子邮件。下面的例子显示了我试图实现的目标。如何编写一个快速的PHP脚本,为我插入计划,更改每次插入的日期

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 (!date2)', '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');
试试这个

<?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;
  }

?>
$date=新的日期时间(“星期一”);//今天还是下周一
$end=新日期时间(“现在+10年”);
//从现在到未来10年的每个星期一,插入一份时间表
而($date<$end){
插入计划表(日期);
$date->modify(‘下周一’);
}
或命令式:

$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');
}
$date=date\u创建(“星期一”);//今天还是下周一
$end=创建日期(“现在+10年”);
//从现在到未来10年的每个星期一,插入一份时间表
而($date<$end){
插入计划表(日期);
日期(日期,下周一);
}

未测试,但应给出大致的想法:

$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) . ');';
$init=strotime('2011-08-15');
$stop=STROTIME('2013-08-15');
$step=604800;
开关(日期('w',$init)){
案例0:
$correction=1;
打破
案例1:
$correction=0;
打破
案例2:
$correction=6;
打破
案例3:
$correction=5;
打破
案例4:
$correction=4;
打破
案例5:
$correction=3;
打破
案例6:
$correction=2;
打破
}
$init+=$correction*$step;
$qry='插入'lm_schedule'('id','type','date','subject','message','htmessage','fattach','list`)值(';
$dates=array();
对于($timestamp=$init;$timestamp<$stop;$timestamp+=$step){
$dates[]='..'.date($Y-m-d',$timestamp)。'..';
}
$qry.=内爆('),(',$dates);

你能解释一下这个月的每个星期一吗?它和每周一一样吗?是的,对不起,这就是我的意思
$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) . ');';