如何知道在php中是否已经过期/过期?

问题描述:

这里的情景如何知道在php中是否已经过期/过期?

我想知道,如果像“2011-03-04”和时间的日期一样'17:06:00'特定的时区“IST/PDT/CDT'已通过使用PHP?

是否有任何可用的功能来检查这个

$strDate = '2011-03-04 17:06:00'; 
$TZ  = 'IST'; 

$strDate = '2011-02-01 11:16:30'; 
$TZ  = 'CDT'; 

$strDate = '2010-08-04 07:20:00'; 
$TZ  = 'IST'; 

$strDate = '2011-02-27 09:55:00'; 
$TZ  = 'PST'; 

我试过/找遍了网,但没有喜悦,请大家帮忙

是啊,我尝试过使用日期时间但它不为我工作。我的服务器是在IST时区

$date = new DateTime('2011-03-04 17:06:00'); 
$date->setTimeZone(new DateTimeZone("PST")); 
$newtime = $date->getTimestamp(); 
$time  = time(); 
if ($time > $usertime) 
    echo 'time passed'; 
else 
    echo 'time NOT passed'; 

感谢TOB YS但它也falied

$elapsedTime = new DateTime(
    '2011-03-04 17:00:00', // I set it to 5PM. Now the time is 5:45 PM here in INDIA 
    new DateTimeZone('IST') 
); 

$elapsedInt = $elapsedTime->diff(new DateTime()); 
echo ($elapsedInt->invert ? 'Future' : 'Past') . "\n"; 
+0

你尝试['日期时间:: createFromFormat()'](HTTP://www.php。净/手动/ EN/datetime.createfromformat.php)? – jensgram 2011-03-04 11:57:47

+0

您必须在构建DateTime对象期间指定时区。否则,它将在您的服务器时区中创建,并且setTimeZone()调用将重新计算要移入外部时区的现有时间。 – tobyS 2011-03-04 12:06:31

+0

[检查时区中的时间是否经过了PHP?]可能的重复(http://*.com/questions/5192065/check-if-a-time-in-a-timezone-is-elapsed-in -php) – 2011-03-04 12:19:05

你可以从你的时间数据创建一个DateTime对象并使用其diff()方法对当前的时间。

示例代码:

<?php 

$elapsedTime = new DateTime(
    '2010-08-04 07:20:00', 
    new DateTimeZone('IST') 
); 

$notElapsedTime = new DateTime(
    '2012-08-04 07:20:00', 
    new DateTimeZone('IST') 
); 

$elapsedInt = $elapsedTime->diff(new DateTime()); 

echo ($elapsedInt->invert ? 'Future' : 'Past') . "\n"; 

$notElapsedInt = $notElapsedTime->diff(new DateTime()); 

echo ($notElapsedInt->invert ? 'Future' : 'Past') . "\n"; 

?> 
+0

@tobyS谢谢,你能否检查这个$ elapsedTime = new DateTime('2011-03-04 17:20:00',new DateTimeZone('IST'));它返回'Future'而不是'Past' – sam 2011-03-04 12:10:58

+0

嗯,IST被解释为+0200。这是正确的抵消? IST未在http://php.net/manual/en/timezones.php上列为有效时区名称。也许你可以切换到支持的名称? – tobyS 2011-03-04 12:18:31

+0

当您切换到“欧洲/柏林”时,您所指定的时间将转换为“星期五,2011年3月4日17:20:00 +0200”,转换为“星期五,2011年3月4日16:20:00 +0100” ”。 – tobyS 2011-03-04 12:19:32

PHP不明白这些时区:

$timezone = 'America/New_York'; 
$date = new DateTime($strDate, new DateTimeZone($timezone));