Page 1 of 1

strtotime() not reading string from var !!? Mind-bottling..

Posted: Tue May 01, 2007 3:00 pm
by SFBILL
I've used the strtotime() function successfully many times. I'm running into a really strange error in which strtotime() is not interpreting a string stored in an array. If I echo the value and cut and paste it into the strtotime() function, it interprets it no problem. I cannot figure out why it isn't interpreting the array value...

Code: Select all

echo "Departing time pulled is: ".$record['depart_time']."<br>";
	$departing = strtotime($record['depart_time']);
echo "Departing time string is: ".$departing."<br><br>";

echo "Departing time hardcoded is: January 12, 2007 6:20AM<br>";
	$departing = strtotime('January 12, 2007 6:20AM');
echo "Departing time string is: ".$departing."<br>";
And the result is...

Code: Select all

Departing time pulled is: January 12, 2007 6:20AM
Departing time string is:

Departing time hardcoded is: January 12, 2007 6:20AM
Departing time string is: 1168600800
There is probably something very basic I am missing here, but after a couple of hours on this I have to swallow my pride and see if someone can help. I am pulling string data in from various sources and need to be able to use the strtotime() function for this. Thanks for any help you can offer.

Bill

Posted: Tue May 01, 2007 3:09 pm
by RobertGonzalez
Do this please:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

echo '<pre>'; var_dump($record); echo '</pre>';
echo 'The array value for the key $record["depart_time"] is ' . $record['depart_time'] . '<br />';
echo 'Passed to the strtotime() function it looks like ' . strtotime($record['depart_time']) . '<br />';
?>

Result - still nothing

Posted: Tue May 01, 2007 3:47 pm
by SFBILL
OK - thanks. I tried:

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

echo '<pre>'; var_dump($record); echo '</pre>';
echo 'The array value for the key $record["depart_time"] is ' . $record['depart_time'] . '<br />';
echo 'Passed to the strtotime() function it looks like ' . strtotime($record['depart_time']) . '<br />';
And got...

Code: Select all

array(9) {
  ["date"]=>
  string(24) "Friday, January 12, 2007"
  ["number"]=>
  string(22) "
Delta Air Lines # 977"
  ["route"]=>
  string(74) "
San Francisco International (SFO) to Atlanta Hartsfield-Jackson ATL (ATL)"
  ["depart_airport"]=>
  string(3) "SFO"
  ["depart_time"]=>
  string(24) " January 12, 2007
6:20AM"
  ["arrive_airport"]=>
  string(3) "ATL"
  ["arrive_time"]=>
  string(30) " January 12, 2007
6:20 AM PST "
  ["class"]=>
  string(7) "Economy"
  ["seat"]=>
  string(5) "
18C "
}

The array value for the key $record["depart_time"] is January 12, 2007 6:20AM
Passed to the strtotime() function it looks like

Posted: Tue May 01, 2007 3:59 pm
by RobertGonzalez
It looks like there is a carriage return or new line in the date (see how it wraps to the next line?). That might be messing with the way it is handled in strtotime. Look in your database and see if there is a \n or \r in there anywhere, and if there is, remove it from the date.

Thank you

Posted: Tue May 01, 2007 5:07 pm
by SFBILL
Well - I knew it would be something basic. Thank you very much, that was it... As a newb I didn't know that echo wouldn't show the newline character in browser output.

I already had a routine in there that is supposed to strip out \n and \r - I'll have to figure out why that didn't do it.

I changed the code as follows and it works (feel free to suggest a better fix.) Thanks again!

Code: Select all

//********************************OLD CODE THAT DID NOT WORK
//$departing = strtotime($record['depart_time']);

//********************************NEW CODE THAT WORKS
//$departing = strtotime(preg_replace('([\r\n])','', $record['depart_time']))

echo '<pre>'; var_dump($record); echo '</pre>';
echo 'The array value for the key $record["depart_time"] is ' . $record['depart_time'] . '<br />';
echo 'Passed to the strtotime() function it looks like ' . strtotime(preg_replace('([\r\n])','', $record['depart_time'])) . '<br />';

Code: Select all

array(9) {
  ["date"]=>
  string(24) "Friday, January 12, 2007"
  ["number"]=>
  string(22) "
Delta Air Lines # 977"
  ["route"]=>
  string(74) "
San Francisco International (SFO) to Atlanta Hartsfield-Jackson ATL (ATL)"
  ["depart_airport"]=>
  string(3) "SFO"
  ["depart_time"]=>
  string(24) " January 12, 2007
6:20AM"
  ["arrive_airport"]=>
  string(3) "ATL"
  ["arrive_time"]=>
  string(24) " January 12, 2007
6:20AM"
  ["class"]=>
  string(7) "Economy"
  ["seat"]=>
  string(5) "
18C "
}

The array value for the key $record["depart_time"] is January 12, 2007 6:20AM
Passed to the strtotime() function it looks like 1168600800

Posted: Tue May 01, 2007 5:14 pm
by RobertGonzalez
I would tend to look at fixing it in the database (and at INSERT time) so that the code doesn't have to run that process. I would also look at using str_replace() with an array of '\r\n', '\n', '\r'. It will be less resource intensive as preg_replace().

OK - this is before it's written to the DB

Posted: Tue May 01, 2007 5:24 pm
by SFBILL
Will do on the str_replace() - thanks.

I'm processing a long string containing flight confirmation information, so this is before it gets into the DB. In essence, this is the process that takes care of it before it hits DB as this is the section that parses out all of the values from the string and INSERTS them to the DB. Some of the formats are fragmented and might not have the year associated with the month, day, and time - so I end up putting them together to form a time that goes into the DB.

As always, I'm definitely open to suggestions on ways to improve this - I really appreciate the help!

Bill

Posted: Tue May 01, 2007 5:31 pm
by superdezign
Mind "bottling?" ... I believe it's "boggling."

Posted: Tue May 01, 2007 5:42 pm
by RobertGonzalez
Not if you've seen that new Will Farrell movie.
Did you just mind bottling?
Yeah, you know like when your thoughts get all trapped, like in a bottle?

Posted: Tue May 01, 2007 5:46 pm
by superdezign
Everah wrote:Not if you've seen that new Will Farrell movie.
Did you just mind bottling?
Yeah, you know like when your thoughts get all trapped, like in a bottle?
Haha, which is that? Stranger Than Fiction (didn't finish watching.. keep falling asleep during slow parts)? Blades of Glory (would like to see ^_^)?

Posted: Tue May 01, 2007 6:00 pm
by RobertGonzalez
Blades of Glory (thanks for that, I could not remember the name).

Now back on topic.

Fun digression - see the movie... str_replace snippet

Posted: Tue May 01, 2007 8:51 pm
by SFBILL
Finally -

Code: Select all

$order   = array("\r\n", "\n", "\r");
$departing = strtotime(str_replace($order,"", $record['depart_time']));
*************************
PS - Glad someone got the 'mind-bottling' reference... great movie for some mindless laughs. Plus mind-bottling is a better description for a simple situation like this that I can't figure out. Thanks for the bottle opener - now, does anyone have bigger bottle?