如何strtotime直到另一个strtotime?

时间:2011-08-22 03:40:19

标签: php

我现在还在学习php语言,我想做的是在if..else语句中,但在if语句中我需要检查thistime直到这个时间只有可用的其他错误,我能知道如何让这个代码完成了吗?

我的代码:

$thistime=date("h:i:s",strtotime("10:00 am"));
$untiltime=date("h:i:s",strtotime("11:00 am"));

if(){ echo blablabla; } else{echo error;};
谢谢你,希望你的家伙尽快回复我。

2 个答案:

答案 0 :(得分:4)

我假设您尝试每天只允许访问某些内容一段时间,特别是在上午10点到上午11点之间。如果是这样,那么你就是在正确的轨道上,但我会尝试为你澄清一些事情。

strtotime()函数将字符串解释为给定的秒数,并返回Unix时间戳(自UTC时区1970年1月1日午夜起的秒数)。您可以使用这些时间戳设置时间范围的开始和结束标记。

date()函数用于将时间戳格式化为特定格式。代码中的h:i:s格式指的是当天的当前小时,分钟和秒,每个值之间都有冒号分隔符。虽然这个功能很有用,但它对你正在尝试做的事情没有任何意义,因为时间戳是比较的更好选择。

您需要了解的最后一项功能是time()。此函数在代码运行时返回Unix时间戳。您会将此值与使用strtotime()定义的起点和终点进行比较,以确定是否显示您的页面。

将所有这些放在一起,我们得到以下代码:

// define the range in which the page is allowed to display
$rangeStart = strtotime("10:00 AM");
$rangeEnd   = strtotime("11:00 AM");

// define a variable with the current time
$now = time();

// compare the current time to the two range points
// to determine if the current time is within the display range

if ($now >= $rangeStart && $now <= $rangeEnd) {
  // display page
} else {
  // display error message
}

请注意,在上面的代码strtotime()中,因为没有给出日期信息,所以假设10:00 AM11:00 AM是当天的时间。这意味着该代码可在每天上午10点到11点之间访问。如果这不是你想要的,那么你必须对你的终点有所了解。另请注意,strtotime()会对服务器设置的任何时区起作用。这意味着如果您的服务器设置为太平洋时间,并且您在纽约,那么从您的角度来看,您将只能在当地时间下午1点到2点之间访问该站点。

答案 1 :(得分:1)

很简单,

 $thistime = date("h:i:s", strtotime("10:00 am"));
 $untiltime=date("h:i:s", strtotime("11:00 am"));

 if(strtotime("10:00 am") < strtotime("11:00 am"))
 { 
     echo "blablabla"; 
 } else {
     echo "error";
 };