比较php IF(语句中的字符串问题


Comparing Strings in IF( statement in php issue

我还没有在网上找到解决方案,因为我认为这是我使用日期格式的方式。

问题:我有一个日期格式:2013-06-07这个格式是由下面创建的:

$thedate = (string) substr($item->children('http://www.w3.org/2005/Atom')->updated,0,-15) . ' - '; -从RSS提要中获取,并修剪为仅表示年、月和日。

我然后使用这个,得到相同的,每个是今天的日期:

$day = (string) date('Y-m-d');

所以当我执行IF语句时,在它进入数据库之前,日期应该匹配:

if($day == $thedate) {
echo ($day . ''n' . $thedate);
$sql = "INSERT INTO rssFeed (date, stage, title, description, location, billtype, link) VALUES('". 
$thedate ."','". $stage . "','" . $title . "', '" . $desc ."', '" . $location ."', '" . $billtype ."', '". $linkurl ."')";
//print_r($sql);
$inres = $mysqli->query($sql);
print_r($mysqli->error);
} else {
echo ('They do not match'); 
}'

如果没有IF语句,它工作得很好,并且我已经打印了两个日期,它们是相同的,但是,无论出于何种原因,该语句出现为,不相同,只有' they do not match'在控制台中,或放入数据库中。没有错误。

有什么想法吗?

试试这个:

$day = date("Y-m-d"); //get rid of the (string) cast
$thedate = date("Y-m-d", strtotime($thedate));
if($day == $thedate) {
    ... // your stuff
} else {
    echo ('They do not match.');
}

在字符串的编码/存储方式上可能有一些细微的差异。由于您的$day变量是用date()函数创建的,因此通过对$thedate执行相同的操作,您更有可能正确匹配它们。