检查日期字符串是否是当前日期

时间:2015-04-09 21:24:06

标签: php

我正在抓很多日期,我想从今天开始检查它们。

类似

if ("Apr 9, 2015 18:45" ==  date('Y-m-d ))

如何查看字符串日期是否为

 Apr 9, 2015 18:45

今天?

我正在检查的日期列表

 Apr 9, 2015 23:40
 Apr 9, 2015 18:45
 Apr 9, 2015 14:18
 Apr 9, 2015 13:23
 Apr 9, 2015 12:24
 Apr 9, 2015 11:00
 Apr 8, 2015 21:15
 Apr 8, 2015 21:15
 Apr 8, 2015 19:35
 Apr 8, 2015 18:54
 Apr 8, 2015 17:09
 Apr 8, 2015 16:41
 Apr 8, 2015 16:30
 Apr 8, 2015 15:55
 Apr 8, 2015 14:15
 Apr 8, 2015 12:38
 Apr 8, 2015 12:12
 Apr 7, 2015 22:30
 Apr 7, 2015 22:15
 Apr 7, 2015 18:26

循环片段

foreach($gosu->find("//table[@class='simple gamelist medium']/tbody/tr") as $gosu_element) {



    $gosu_date = mysql_real_escape_string($gosu_element->find("/td[@class='live-in']", 0));
    if (date('Y-m-d', strtotime($gosu_date)) ==  date('Y-m-d')){
        echo date('Y-m-d', strtotime($gosu_date)) . "<br>"; 
    }


}

var_dump输出:

string(0) "" string(44) "Apr 9, 2015 23:40" string(44) "Apr 9, 2015 18:45"   string(44) "Apr 9, 2015 14:18" string(44) "Apr 9, 2015 13:23" string(44) "Apr 9, 2015 12:24" string(44) "Apr 9, 2015 11:00" string(44) "Apr 8, 2015 21:15" string(44) "Apr 8, 2015 21:15" string(44) "Apr 8, 2015 19:35" string(44) "Apr 8, 2015 18:54" string(44) "Apr 8, 2015 17:09" string(44) "Apr 8, 2015 16:41" string(44) "Apr 8, 2015 16:30" string(44) "Apr 8, 2015 15:55" string(44) "Apr 8, 2015 14:15" string(44) "Apr 8, 2015 12:38" string(44) "Apr 8, 2015 12:12" string(44) "Apr 7, 2015 22:30" string(44) "Apr 7, 2015 22:15" string(44) "Apr 7, 2015 18:26"

2 个答案:

答案 0 :(得分:1)

strtotime()非常方便:

if (date('Y-m-d', strtotime("Apr 9, 2015 18:45")) ==  date('Y-m-d')){

这将导致类似的字符串。输入将与现在"2015-04-09" == "2015-04-09"进行比较。

http://php.net/manual/en/function.date.php

我会做类似的事情:

<?php
$dates = array(
  "Apr 9, 2015 23:40",
  "Apr 8, 2015 18:45",
  "Apr 7, 2015 14:18"
);
$num_true = 0;
$today = date('Y-m-d');

for($i=0;$i<=count($dates);$i++){
  if(date('Y-m-d', strtotime($dates[$i])) != $today){
    unset($dates[$i]);
  }
}
// Array only has dates that are equal to today.

echo "There are " . count($dates) . " dates that match today:\r\n";
foreach($dates as $day){
  echo "$day\r\n";
}
?>

编辑:FOR语句中的错误逻辑。

答案 1 :(得分:0)

只需创建一个DateTime对象和format就可以比较它,如下所示:

if ((new DateTime("Apr 9, 2015 18:45"))->format("Y-m-d") ==  date("Y-m-d"))