Page 1 of 1

Strange char-to-int problem

Posted: Mon Aug 15, 2011 11:11 am
by jorny62
Hello,

I have a strange problem with converting a char to int. The program read a CSV file containing several fields separated with ;. One of the column contain a unix timestamp value. The code I have written to parse this file work OK on my computer (Fedora Core 12 using PHP 5.2.12). But on a different system (lets call it X - I have not found out about version on that system yet), it does not work.

Based on the information it seems like the unix timestamp value is stored in a 20-byte long character on system X, while on my system it is stored as a 10-byte character. Based on this I wrote a short program to test things:

Code: Select all

var_dump(iconv_get_encoding('all'));

$fromTime = 1306890000;
var_dump($fromTime);

$x = "$fromTime";
var_dump($x);

$xx = date('Y-m-d', $x);
var_dump($xx);

$y = iconv('UTF-8', 'UTF-16LE', $x);
var_dump($y);

$xx = date('Y-m-d', (int)$y);
var_dump($xx);

echo $y + 1 - PHP_EOL;
echo intval($y, 10) . PHP_EOL;
which output:
[text]
array(3) {
["input_encoding"]=>
string(10) "ISO-8859-1"
["output_encoding"]=>
string(10) "ISO-8859-1"
["internal_encoding"]=>
string(10) "ISO-8859-1"
}
int(1306890000)
string(10) "1306890000"
string(10) "2011-06-01"
string(20) "1306890000"
string(10) "1970-01-01"
21
[/text]

So my question is, how to convert a the 20-byte string containing the number to int? Nothing seem to work.

Re: Strange char-to-int problem

Posted: Mon Aug 15, 2011 3:14 pm
by Christopher
My guess is that date() only accepts 8 bit strings. Check the docs. I would recommend converting all of your timestamps to 8 bit.