【问题标题】:Working out future dates in PHP from days of week and times从星期几和时间计算出 PHP 中的未来日期
【发布时间】:2015-02-09 10:09:39
【问题描述】:

我有以下数组,其中包含星期几 (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);
}

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

谢谢!

【问题讨论】:

  • 只需编写一个函数并在数组中循环,这没什么大不了的。
  • 是的,循环很简单。它将星期几和时间转换为 unix 日期,无论是在过去还是将来,它都可以工作。我会在问题中更清楚地说明这一点。

标签: php date datetime


【解决方案1】:

要操作日期,您可以使用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);

【讨论】:

    【解决方案2】:

    只需“询问”strtotimeDateTime

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

    【讨论】:

      猜你喜欢
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多