我有以下数组,包含星期几(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);
}
我无法解决如何实现上述目标。任何帮助将不胜感激。
谢谢!
答案 0 :(得分:0)
要操纵日期,您可以使用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);
答案 1 :(得分:0)
只是&#34;问&#34; 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