【发布时间】:2011-02-24 09:02:00
【问题描述】:
基本上,我需要将当前/未来的工作日转换为时间戳。
例子:
today = Thu, 24 Feb 2010
weekday = Tue
next date = Tue, 1 Mar 2010
cur stamp = 1267016400
new stamp = 1267448400
【问题讨论】:
基本上,我需要将当前/未来的工作日转换为时间戳。
例子:
today = Thu, 24 Feb 2010
weekday = Tue
next date = Tue, 1 Mar 2010
cur stamp = 1267016400
new stamp = 1267448400
【问题讨论】:
strtotime('Tue') 将返回下周二的时间戳。
strtotime('Tue', $time) 将从给定的时间戳返回下周二的时间戳。
【讨论】:
这是我最终得到的代码:
/**
* @param $weekday string The weekday in short (3 letter) format, eg: "Mon" or "Tue".
* @return integer The calculated timestamp.
*/
function next_weekday_to_stamp($weekday,$today=null){
if(!$today)$today=time();
$range = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$days = array_search(date('D'), $range);
$range = array_slice($range, $days);
$days = array_search($weekday, $range);
return strtotime('+'.$days.' days', $today);
}
谈论过度设计!
无论如何,如果有人想在过去(上个工作日)使用它,它可以很容易地适应。
【讨论】:
你试过了吗
strtotime("+7 days", strtotime("Thu, 24 Feb 2010"))
【讨论】: