【问题标题】:Php Date and Time ComparisionPHP日期和时间比较
【发布时间】:2012-09-13 22:50:41
【问题描述】:

我看过this的问题,但时间格式不同

我有以下日期格式Tue, 11 Sep 2012 17:38:09 GMT$pubDate变量中

我想将 $pubDate 与当前日期和时间进行比较,以查看 Tue, 11 Sep 2012 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;
                }

它给出警告: 依赖系统的时区设置是不安全的。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种,但仍然收到此警告,您很可能拼错了时区标识符。我们在第 26 行的 /Users/xxxxx/Desktop/xxxx/xxxx/xxxx.php 中为“EDT/-4.0/DST”选择了“America/New_York”

编辑:

我已将date_default_timezone_set('America/New_York'); 行添加到我的 php 中,现在

$inDate  = DateTime::createFromFormat( $format, $pubDate);
    $postDate = new DateTime();

    $diff = $inDate->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');
    }

在没有警告的情况下工作,谢谢大家

【问题讨论】:

  • 那么...你当时调用date_default_timezone_set()函数了吗?
  • 附带说明,如果您将strtotime($pubDate)strtotime('-10 minutes') 进行比较会更容易......
  • @veredesmarald 我没有打电话给date_default_timezone_set()

标签: php date time


【解决方案1】:

使用DateTime进行比较:

$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');
}

您可以在this demo 中使用它并查看它的工作情况。

【讨论】:

  • +1 用于使用 DateTime 代替旧的 datetime 函数
  • 弗洛伦特,感谢您的提示。我通常避免构造函数,我不知道为什么。感谢 Havelock,DateTime 类是一个几乎未被发现的宝石。
  • 您失去了 DateTime 将每个元素与字符串进行比较的潜力。使用 DateTime +1,你的if -100
  • @Maks3w - 什么元素正在与字符串进行比较?
  • @nickb ->format 的输出是一个字符串。您从字符串转换为 DateTime 以再次转换为字符串并进行比较
【解决方案2】:

您可以比较两个 DateTime 对象。

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

if ($myTime >= $nowLessTenMinutes);

【讨论】:

  • 这个答案更像是 PHP 5.3 风格
  • 这怎么行,if 的参数是integer &gt;= object。正如预期的那样,它以Notice: Object of class DateTime could not be converted to int 失败。
  • $myTime 是一个 DateTime 对象。我省略了如何从字符串传递到 DateTime。我认为我们不应该给出完整的答案并给出提示
【解决方案3】:

使用DateTime::diff()计算差值:

$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' );

【讨论】:

    【解决方案4】:

    我有同样的问题,如果你使用 MAMP 或类似的东西,更改 php.ini 会很复杂尝试在你的 php 文件顶部添加date_default_timezone_set('America/New_York');

    那么这个线程上的大多数其他答案应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多