A webservice returns a timestamp field in base64Binary format. It looks like this in SOAP response:
<a:TimeStamp>AAAAAAMpI9Q=</a:TimeStamp>
PHP __soapCall, however, b64_decode()s that and I get a binary string looking like ')#▒'. How do I get actual timestamp out of this? I tried to unpack('L') it but it gives me Array([1] => 0) as a result. Is there really zero i.e. 1970-01-01 or have I missed something?
This test program:
$b = "AAAAAAMpI9Q=";
$ts = base64_decode($b);
print_r(array_map("ord", str_split($ts)));
outputs:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 3
[5] => 41
[6] => 35
[7] => 212
)
showing that the base64-encoded string gives you an 8-character string when unpacked. So presumably it represents a 64-bit integer, which might be signed or unsigned, and no, it isn't zero.
Given the values above it looks like the value is 53027796 - is that what you're expecting?
Related
i've a string like
$input="16°28'60,00''"
thats is on my db and stored as TEXT utf8_general_ci
im trying to convert it to decimal/lat-long system. So I write a function that splice the input and convert it.
Im using $input as an array, and when is on position 2, I have a strange result thats broke my function:
$input[2]---> 'b"Â"'
in position 2 there is the "°"
the next row check if esist "°" but due this error can works
if($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", '°')
how can i fix that?
If the format of the DB string is always the same, just grab the digits out and you don't need to bother with the degrees, minutes, seconds.
$input = "16°28'60,00''";
preg_match_all("/(\d+)/", $input, $match);
print_r($match);
Output:
Array
(
[0] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
[1] => Array
(
[0] => 16
[1] => 28
[2] => 60
[3] => 00
)
)
Now you have each digit and you can convert it easily.
I baked something on cake bake and entering values on local host in that application. The name of the value that i want to retrieve for php script is temperature. how can i get that value?
Here is the some information laying behind the application:
[useDbConfig] => default
[useTable] => temperature_readings
[id] => 32
[data] => Array
(
[TemperatureReading] => Array
(
[temperature] => 15
[location_id] => 5
[created] => 1437572170
[id] => 32
that temperature value, which is 15, is the variable that i want to use in php script.
You can access temperature like this:
$temperature = $array['data']['TemperatureReading']['temperature'];
echo $temperature; //Returns 15
I have some strange behavior with the unpack function. I have a packed string, stored as longblob in a mysql database. When I read that string and unpack it, it gives me an array, so far so good. But when I run this on another machine some of the values in the array are different.
When I dump the data from mysql, they are equal on both machines.
Unpacking is done this way:
$array = unpack("N*", $packed);
$array should then be like this (and it is on one machine)
Array
(
[1] => 179848175
[2] => -16214255
[3] => 179848175
[4] => -16214255
[5] => 179848175
[6] => -16214255
[7] => 179999949
[8] => -16152916
[9] => 179999277
[10] => -16168574
...
)
But on the other machine it is like this:
Array
(
[1] => 179848175
[2] => 427853622
[3] => 179848175
[4] => 427853622
[5] => 179848175
[6] => 427853622
[7] => 179999949
[8] => 427853423
[9] => 179999277
[10] => 427853341
...
)
Every second value seems to be different.
I have tested this on three different machines, on two everything was fine, but on that one machine I get that weird output.
One machine is running PHP 5.6.3 (here it is ok), two machines are running PHP 5.5.14 (on one it is ok, on the other not)
The pack format N means unsigned long, which means it can't be negative. However, you are storing negative values, and they are the ones that aren't being unpacked the way you want. PHP does not have a pack format for machine-independent signed longs; it only supports packing them in machine byte order, which may not be compatible from machine to machine. Thus you'll have to make the values signed yourself.
To convert your array items into signed values:
for ($i = 1; $i <= count($array); $i++) {
// Check for a greater-than-32-bit environment,
// and check if the number should be negative
// (i.e., if the high bit is set in 32-bit notation).
if (PHP_INT_SIZE > 4 && $array[$i] & 0x80000000) {
// A negative number was unpacked as an unsigned
// long in a greater-than-32-bit environment.
// Subtract the appropriate amount (max 32-bit
// unsigned long + 1) to convert it to negative.
$array[$i] = $array[$i] - 0x100000000;
}
}
var_dump($array);
From the JSON snippet that i got from a .net API query, I can't seem to convert the date /Date(1393477200000)/ properly in PHP.
I tried to do echo date('m/n/Y','1393477200000'); but it is still outputting the wrong date which is 07/7/46127 instead of the correct date of 2/27/2014.
Array
(
[status] => ok
[results] => Array
(
[0] => Array
(
[PROJECT_ID] => 1
[COMPANY_ID] => 1
[PROJECT_NAME] => The "Getting Started" Project
[PROJECT_NUMBER] => 000001
[CAN_OPEN_PROJECT] => 1
[DATE_START_DATE] => /Date(1393477200000)/
[DATE_END_DATE] => /Date(1440648000000)/
[PROJECT_DESC] =>
[TASK_NUMBER] => 6
[DATE_CREATED] => /Date(1409142925980)/
[TOTAL_TASKS] => 1
[TOTAL_INCOMPLETE_TASKS] => 1
[TOTAL_COMPLETED_TASKS] => 0
Any ideas how to format [DATE_START_DATE] correctly in PHP? Thanks!
Divide your unix-timestamp by 1000, then use date(..).
$date = '1393477200000';
echo date("m/d/y", $date/1000);
Its that simple.
Result:
02/27/14
echo date('m/d/Y',(1393477200000/1000)); U have to truncate last 3 digit.
The time received is in milliseconds... You have to divide by a thousand to retrieve UNIX epoch time:
echo date('m/d/Y',(1393477200000/1000));
And here is the fiddle
I have the following code:
$this->api = new App_Other_SphinxSearch();
$this->api->SetServer($host, $port);
$this->api->SetConnectTimeout(1);
$this->api->SetArrayResult(true);
$results = $this->api->Query("#(title,content) test", 'members');
echo "<pre>";print_r($results);die;
According to their documentation, a syntax like #(field_1,field_2) query should return docs which match the string query in either field_1 or field_2.
The PHP SDK returns something entirely different:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => content
)
[attrs] => Array
(
[created] => 2
[content] => 7
)
[total] => 0
[total_found] => 0
[time] => 0.000
[words] => Array
(
[title] => Array
(
[docs] => 10
[hits] => 34
)
[content] => Array
(
[docs] => 34
[hits] => 139
)
[test] => Array
(
[docs] => 26
[hits] => 34
)
)
)
There is no matches key in the array, however it got some hits. I don't really understand why this is happening, especially because if I try the same query from the command line, everything works correctly.
Any help?
Edit: Querying like this works though: #* test. Not really what I want though, cause that searches in all fields.
[total_found] => 0 says there where no matches.
The words array, just tells you now many documents and how many times that word appears in ANY (and all) fields. (WITHOUT regard to your specific query)
Sphinx caches those word stats, which help it make quick sanity checks on queries. When the query runs it can end up not matching any documents (because only then does it apply the field level filters), even though the individual words are found.
That explains your misinpretation of the results, but not why getting the results you get.
You are entering a Extended Mode query (as evidenced by the link to the sphinx documentation) , but the sphinx API defaults to ALL query mode. Notice also that title and content are in the words array, so are being taken as plain keywords not as syntax.
So you need to include a
$this->api->SetMatchMode(SPH_MATCH_EXTENDED);
btw, #* test, works because the #* are simply ignored in ALL query mode.