我有一个unix时间戳,我希望能够测试它是否发生在当周星期日凌晨12:00之前。有什么建议吗?
答案 0 :(得分:3)
<?php
/**
* @param $t UNIX timestamp
* @return true iff the given time fell in the previous Sunday->Sunday window
*/
function f($t) {
$a = strtotime("last Sunday");
$b = strtotime("-1 week", $a);
return ($b <= $t && $t < $a);
}
var_dump(f(strtotime("2011-08-12 11:00")));
var_dump(f(strtotime("2011-08-08 11:00")));
var_dump(f(strtotime("2011-08-04 11:00")));
var_dump(f(strtotime("2011-08-01 11:00")));
?>
输出:
bool(false)
bool(false)
bool(true)
bool(true)
答案 1 :(得分:1)
我认为您可以使用“生成本周”星期日上午12点“的时间戳 日期时间类:http://www.php.net/manual/en/book.datetime.php
您应该能够为目标日期生成日期时间对象,然后使用gettimestamp:http://www.php.net/manual/en/datetime.gettimestamp.php将其转换为时间戳。
然后,您可以比较该时间戳,看它是否小于您生成的时间戳。
编辑:一些代码(尽管不像Tomalak Geret'kal那样优雅)
<?php
//Get current year and week
$year = date('Y');
$week = date('W');
//Get date
$date = date("Y-m-d", strtotime("$year-W$week-7"));
//Get date with time
$datetime = new DateTime("$date 00:00:00");
//Display the full date and time for a sanity check.
echo $datetime->format('Y-m-d h:i:s') . ' = ';
//Convert to timestamp:
$timestamp = $datetime->getTimeStamp();
echo $timestamp;
//Do your comparison here:
if($yourtimestamp < $timestamp){
return true;
}