Php日期和时间比较

Php日期和时间比较,php,date,time,Php,Date,Time,我看过这个问题,但时间格式不同 我有以下日期格式2012年9月11日星期二17:38:09 GMT 在$pubDate变量中 我想将$pubDate与当前日期和时间进行比较,看看2012年9月11日星期二17:38:09 GMT是否在最后10分钟内 编辑: 我试过了 //get current time strtotime($pubDate); time() - strtotime($pubDate);

我看过这个问题,但时间格式不同

我有以下日期格式
2012年9月11日星期二17:38:09 GMT
$pubDate
变量中

我想将
$pubDate
与当前日期和时间进行比较,看看
2012年9月11日星期二17:38:09 GMT
是否在最后10分钟内

编辑:

我试过了

//get current time
                strtotime($pubDate);
                time() - strtotime($pubDate);
                if((time()-(60*10)) < strtotime($pubDate)){
                    //if true increase badge by one
                    $badge = $badge + 1;
                }
在没有警告的情况下工作,谢谢大家使用进行比较:

$format = 'D, d M Y H:i:s O';
$tz = new DateTimeZone( 'America/New_York');

// Create two date objects from the time strings
$pubDate  = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);
$postDate = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);

// Compute the difference between the two timestamps
$diff = $pubDate->diff( $postDate);

// If the total number of days is > 0, or the number of hours > 0, or the number of minutes > 10, then its an invalid timestamp.
if( $diff->format( '%a') > 0 || $diff->format( '%h') > 0 || $diff->format( '%i') > 10) {
    die( 'The timestamps differ by more than 10 minutes');
}
您可以使用它,并看到它在中工作。

用于进行比较:

$format = 'D, d M Y H:i:s O';
$tz = new DateTimeZone( 'America/New_York');

// Create two date objects from the time strings
$pubDate  = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);
$postDate = DateTime::createFromFormat( $format, 'Tue, 11 Sep 2012 17:38:09 GMT', $tz);

// Compute the difference between the two timestamps
$diff = $pubDate->diff( $postDate);

// If the total number of days is > 0, or the number of hours > 0, or the number of minutes > 10, then its an invalid timestamp.
if( $diff->format( '%a') > 0 || $diff->format( '%h') > 0 || $diff->format( '%i') > 10) {
    die( 'The timestamps differ by more than 10 minutes');
}
您可以使用它,并看到它在中工作。

用于计算差异:

$input = new DateTime( 'Tue, 11 Sep 2012 17:38:09 GMT' );
$now = new DateTime();

/* calculate differences */
$diff = $input->diff( $now );

echo $diff->format( '%H:%I:%S' );
用于计算差异:

$input = new DateTime( 'Tue, 11 Sep 2012 17:38:09 GMT' );
$now = new DateTime();

/* calculate differences */
$diff = $input->diff( $now );

echo $diff->format( '%H:%I:%S' );

您可以比较两个DateTime对象

$nowLessTenMinutes = new DateTime();
$nowLessTenMinutes->sub(new DateInterval('PT10M')); // Sub 10 minutes

if ($myTime >= $nowLessTenMinutes);

您可以比较两个DateTime对象

$nowLessTenMinutes = new DateTime();
$nowLessTenMinutes->sub(new DateInterval('PT10M')); // Sub 10 minutes

if ($myTime >= $nowLessTenMinutes);

我也有同样的问题,如果您使用的是MAMP或类似的东西,那么更改php.ini会很复杂,请尝试添加
date\u default\u timezone\u set('America/New\u York')在php文件的顶部


然后,这个线程上的大多数其他答案都会起作用。

我也有同样的问题,如果您使用MAMP或类似的东西,那么更改php.ini将非常复杂,请尝试添加
date\u default\u timezone\u set('America/New\u York')在php文件的顶部



那么这条线索上的大多数其他答案都会起作用。

所以。。。那么你调用了
date\u default\u timezone\u set()
函数了吗?另一方面,如果你将
strotime($pubDate)
strotime('-10分钟')
。@veredesmarald我没有调用
date\u default\u timezone\u set()
那么。。。那么你调用了
date\u default\u timezone\u set()
函数了吗?另一方面,如果你将
strotime($pubDate)
strotime('-10分钟')进行比较,会更容易些。
…@veredesmarald我没有调用
date\u default\u timezone\u set()
+1用于使用
日期时间
代替旧的
日期
时间
功能Lorent,感谢您的提示。我通常避免使用构造函数,我不知道为什么。多亏了Havelock,DateTime类是一块尚未被发现的宝石。你失去了DateTime将每个元素与字符串进行比较的潜力。如果
-100@Maks3w-正在将哪个元素与字符串进行比较?@nickb->format的输出是一个字符串。您可以将字符串转换为日期时间,以再次转换为字符串,并比较+1,以使用
DateTime
而不是旧的
date
time
函数。谢谢您的提示。我通常避免使用构造函数,我不知道为什么。多亏了Havelock,DateTime类是一块尚未被发现的宝石。你失去了DateTime将每个元素与字符串进行比较的潜力。如果
-100@Maks3w-正在将哪个元素与字符串进行比较?@nickb->format的输出是一个字符串。您可以从字符串转换为日期时间,然后再次转换为字符串并进行比较。答案更像是PHP 5.3样式。这是如何工作的,如果
的参数是
整数>=object
。失败,请注意:DateTime类的对象无法按预期转换为int。$myTime是DateTime对象。我省略了如何从字符串传递到日期时间。我认为我们不应该给出完整的答案并给出提示。这个答案更像是PHP5.3样式。这是如何工作的,如果
的参数是
integer>=object
。失败,请注意:DateTime类的对象无法按预期转换为int。$myTime是DateTime对象。我省略了如何从字符串传递到日期时间。我认为我们不应该给出完整的答案和提示