Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否已过计算时间&;约会?_Php_Date_Time - Fatal编程技术网

Php 是否已过计算时间&;约会?

Php 是否已过计算时间&;约会?,php,date,time,Php,Date,Time,我得检查一下是否已经过了某个时间和日期。 通常这对我来说很容易,但必须检查的时间比我实际从数据库中获取的时间早半个小时 假设数据库显示:“2014-01-16”和“20:00”。在这种情况下,我必须检查它是否已经过了2014年1月16日的“21:30” 我现在有一些代码在为我工作,但它只是说如果日期和时间都已经过去了,那么它就已经过去了(比如说,它是在第二天,所以它显然已经过去了,它也必须在21:30之后才能这么说) 以下是我目前掌握的代码: // Get the date today and

我得检查一下是否已经过了某个时间和日期。 通常这对我来说很容易,但必须检查的时间比我实际从数据库中获取的时间早半个小时

假设数据库显示:“2014-01-16”和“20:00”。在这种情况下,我必须检查它是否已经过了2014年1月16日的“21:30”

我现在有一些代码在为我工作,但它只是说如果日期和时间都已经过去了,那么它就已经过去了(比如说,它是在第二天,所以它显然已经过去了,它也必须在21:30之后才能这么说)

以下是我目前掌握的代码:

// Get the date today and date of the event to check if the customer can still buy tickets
$dateToday = strtotime(date('Y-m-d'));
// $details['Datum'] == 2014-01-15
$dateStart = strtotime($details['Datum']);

// Check if it's half an hour before the event starts, if so: don't sell tickets
$timeNow = strtotime(date('H:i'));
// $details['BeginTijd'] == 20:00
$substrStart = substr($details['BeginTijd'], 0, 5);
$startTimeHalfHour = strtotime($substrStart) - 1800;

if($timeNow > $startTimeHalfHour && $dateToday >= $dateStart) {
    // It's past the given time limit
    $tooLate = true;
} else {
    // There's still time
    $tooLate = false;
}

如您所见,它要求时间和日期都超过给定的限制。在本例中,如果超过15,则应将$tooLate设置为true,或者如果超过15日的21:30。

您可以使用
strotime(“+30分钟”)
以30分钟为单位获取时间

因此,假设$startTime是事件开始的时间(unix时间),您可以

$current = strtotime("+30 minutes");

if($current > $startTime){
$tooLate = true;
}
else{
$tooLate = false;
}
顺便说一句,像
$dateToday=strotime(date('Y-m-d'))这样的行没有多大意义。编写
$dateToday=time()具有相同的结果

strotime()
提供Unix时间戳(自1970年以来的秒数)<代码>时间()
也可以

要生成
$startTime
(节目开始时间),您应该获取完整的日期和时间字符串(例如
2014-01-15 18:10:00
)并将其传递给
strotime
。它会将其转换为Unix时间

如果你想要的是活动时间减去30分钟,你可以写:

$maxTime = strtotime("-30 minutes",$startTime); //where $startTime is the start time of the event, in Unix time.

来源:

最好将日期时间字符串转换为Unix时间戳,以便进行这样的比较。这可以在课堂上完成。例如:

$dateToday = new DateTime();
$date = new DateTime('2014-01-16 20:00');
// Adds an hour to the date.
$date->modify('+ 1 hour');

if ($dateToday > $date) {
  // Do stuff.
}

但是我需要活动开始的时间-30分钟,而不是当前时间+30分钟。在这种情况下,开始时间是什么?最终结果是一样的。您可以检查当前时间+30分钟,也可以检查事件时间减去30分钟。逻辑是一样的。但如果您真的想要event+30,我也在我的答案中添加了它。谢谢,这非常有效:)。我有一个旧的$dateToday=strotime(date('Y-m-d');因为我不太擅长计算日期/时间,所以我在网上查阅了how to's。当您使用不同的资源工作时,代码可能会变得混乱。好吧,但这并不能真正解决我的问题。我仍然需要从限制日期算起30分钟。Unix时间戳表示以秒为单位的时间(从1970年开始)。因此,您可以通过添加1800秒来轻松操纵它。或者您可以使用DateTime的
modify
方法。您不需要转换为时间戳。DateTime对象是可直接比较的,因此这将起作用:-
如果($dateToday>$date){//do stuff}