PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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...
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>";
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.
<?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 />';
?>
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 />';
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
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.
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!
//********************************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 />';
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
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().
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!
*************************
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?