0

I have not found a solution to this online, because I think its the way i am using the date format.

Problem: I have a date format: 2013-06-07 This formate is created by the following:

$thedate = (string) substr($item->children('http://www.w3.org/2005/Atom')->updated,0,-15) . ' - '; - Which is taken from a RSS feed, and trimmed so only the year, month and day is represented.

I then use this, to get the same, each is todays date:

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

So when I do a IF statement, the days should match, before it enters into the database:

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

It works fine without the IF statement, and I have printed both dates and they are identical, but, for whatever reason, the statement comes up as, not the same, and only 'They do not match' in console, or put into the database. No errors.

Any ideas ?

5
  • 1
    try a var_dump instead of just echo, ensure there's no trailing or leading space on your substr version. (Also try just doing 10, not -15?) As a note, you don't need to explicitly cast those as strings. Commented Jun 7, 2013 at 15:26
  • 1
    You used double quotes, so you didn't need the variables concatenated at all. Commented Jun 7, 2013 at 15:26
  • yes, do a var_dump on both and also be sure that they are the same datatype too. They both should be string and have no trailing spaces. Commented Jun 7, 2013 at 15:26
  • 1
    Why are you typecasting date? Commented Jun 7, 2013 at 15:28
  • 1
    The output of date(Y-m-d) is 10 chars long. Even when your substr($x, 0, -15) would be 10 chars long, you additionally append another 3 chars to $thedate (' - '). Hence you compare a 10 vs. 13 chars string, which will fail. Commented Jun 7, 2013 at 15:43

1 Answer 1

1

Try this:

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

There may be some minute differences in the way the string is encoded/stored. Since your $day variable was created with the date() function, you stand a better chance of matching them properly by doing the same with $thedate.

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to have done the trick $thedate = date("Y-m-d", strtotime(substr($item->children('http://www.w3.org/2005/Atom')->updated,10))); Thank you all for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.