如何拖延时间,直到另一个拖延时间


how to strtotime until another strtotime?

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

我代码:

$thistime=date("h:i:s",strtotime("10:00 am"));
$untiltime=date("h:i:s",strtotime("11:00 am"));
if(){ echo blablabla; } else{echo error;};

谢谢你,希望你能尽快回复我

我假设您正在尝试每天只允许在一定时间内访问某些内容,特别是在上午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点之间访问站点。

很简单,

 $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";
 };