根据一周中的几天和时间,用PHP计算出未来的日期


Working out future dates in PHP from days of week and times

我有以下数组,包含星期几(0-6)和以小时、分钟和秒为单位的时间:

$times = array(     array("1","10:04:00"),
                    array("1","14:20:00"),
                    array("1","20:30:00"),
                    array("1","22:22:00"),
                    array("2","10:14:00"),
                    array("2","14:36:00"),
                    array("2","20:12:00"),
                    array("2","22:42:00"),
                    array("3","12:04:00"),
                    array("3","15:20:00"),
                    array("3","21:30:00"),
                    array("4","23:22:00"),
                    array("4","09:04:00"),
                    array("4","12:20:00"),
                    array("4","19:30:00"),
                    array("4","22:32:00"),
                    array("5","13:04:00"),
                    array("5","16:20:00"),
                    array("5","20:10:00"),
                    array("5","22:02:00")
                    );

我如何循环以上内容并确定活动的下一个日期?例如,如果今天是星期二,上面数组中的前几个日期将是:

下周一10:04:00下周一14:20:00下周一20:30:00下周一22:22:00今天上午10:14:00今天14:36:00等等。

一旦循环到达上述数组的末尾,它将循环回到开头,因此需要计算出未来的日期。日期和时间需要转换为unix日期时间。

这是迄今为止的脚本:

$row = 1;
if (($handle = fopen("evergreen.csv", "r")) !== FALSE) {
        $loopy = 0;
        $thisWeek = date("W");
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if(!isset($times[$loopy])) {$loopy = 0;}
        $dayoftheweek = $times[$loopy][0];
        $thetime      = $times[$loopy][1];
        echo "Day: $dayoftheweek - The time: $thetime<br />";
        // How can I work out the next date and time to post from this?
        $loopy++;
  }
  fclose($handle);
}

我不知道如何实现上述目标。如有任何帮助,我们将不胜感激。

谢谢!

要操作日期,可以使用strtotime()。直接来自php.net网站的示例:

<?php
echo strtotime("now"), "'n";
echo strtotime("10 September 2000"), "'n";
echo strtotime("+1 day"), "'n";
echo strtotime("+1 week"), "'n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "'n";
echo strtotime("next Thursday"), "'n";
echo strtotime("last Monday"), "'n";

在您的情况下,您可以将时间放在第二个参数中:

strtotime("+1 day", $timefromloop);

只需"询问"strtotime或DateTime

<?php
$weekdays = array(
    '1'=>'monday',
    '2'=>'tuesday',
    '3'=>'wednesday',
    '4'=>'thursday',
    '5'=>'friday'
);
$currentDay = getdate()['wday']; // for php < 5.4 there is no function array de-reference
$times = array(
    array("1","10:04:00"),
    array("1","14:20:00"),
    array("1","20:30:00"),
    array("1","22:22:00"),
    array("2","10:14:00"),
    array("2","14:36:00"),
    array("2","20:12:00"),
    array("2","22:42:00"),
    array("3","12:04:00"),
    array("3","15:20:00"),
    array("3","21:30:00"),
    array("4","23:22:00"),
    array("4","09:04:00"),
    array("4","12:20:00"),
    array("4","19:30:00"),
    array("4","22:32:00"),
    array("5","13:04:00"),
    array("5","16:20:00"),
    array("5","20:10:00"),
    array("5","22:02:00")
);
$times = array_map(function($e) use ($weekdays, $currentDay) {
        $q = $weekdays[ $e[0] ]; // get the name of the weekday for strtotime
        $q.= ' '.$e[1]; // append the time of the event
        if ( $e[0]==$currentDay ) {
            return strtotime($q);
        }
        else {
            // for any other day than today "ask" strtotime for the next/coming day with that name
            return strtotime('next '.$q);
        }
    },
    $times
);

foreach($times as $ts) {
    echo date('Y-m-d H:i:s', $ts), "'r'n";
}

打印(今天)

2015-02-09 10:04:00
2015-02-09 14:20:00
2015-02-09 20:30:00
2015-02-09 22:22:00
2015-02-10 10:14:00
2015-02-10 14:36:00
2015-02-10 20:12:00
2015-02-10 22:42:00
2015-02-11 12:04:00
2015-02-11 15:20:00
2015-02-11 21:30:00
2015-02-12 23:22:00
2015-02-12 09:04:00
2015-02-12 12:20:00
2015-02-12 19:30:00
2015-02-12 22:32:00
2015-02-13 13:04:00
2015-02-13 16:20:00
2015-02-13 20:10:00
2015-02-13 22:02:00