I am using PHP to query an AS400 DB2 database. The time(ADACTM) is saved in the table like;
I need to convert this to a human-readable format like 9:46:23.
I am currently doing this in PHP;
$adactm = str_split($fin2['ADACTM']);
$adactm = "$adactm[0]$adactm[1]:$adactm[2]$adactm[3]:$adactm[4]$adactm[5]";
The problem is, when the time doesn't have a 2-digit hour, PHP thinks array position 0 is actually position 1. So the time shows like;
94:62:3
If anyone has a way to fix this, it would be greatly appreciated. Thanks!
Pad the string before you split it:
$rawtime = '94623';
$padded = str_pad($rawtime, 6, '0', STR_PAD_LEFT); // 094623
Then split/mangle as before
Sorry, no better idea than this:
$adactm = str_split($fin2['ADACTM']);
if (count($adactm) == 5) {
$adactm = "$adactm[0]:$adactm[1]$adactm[2]:$adactm[3]$adactm[4]";
} else {
$adactm = "$adactm[0]$adactm[1]:$adactm[2]$adactm[3]:$adactm[4]$adactm[5]";
}
Related
Hello my dear coding friends.
I have a time, formatted like this 08:00:00. That time comes from my phpMyAdmin database where i have a "time" field and i get that field by using a query in my php code. The variable type of that mysqli variable containing the time is string, so i want to cut the minutes and seconds part off and turn the rest into an integer by adding (int). The code looks like this: Image of code
if (strpos ($meetings["dtStartZeit"], "0") == 0) {
$startTimeString = substr ($meetings["dtStartZeit"], 1, 1);
} else {
$startTimeString = substr ($meetings["dtStartZeit"], 0, 2);
}
$startTimeNumber = (int)$startTimeString;
Now comes the confusing part. If i have a string like this --> "8" and I want to turn it into an integer by using the above mentioned function, the result is 9 and not 8. The even more confusing part is that if I increase the value of that variable by 1, the result is 8.
Can someone explain me this please?
You don't need to use strpos or substr here. Use a single line type cast instead all of your code:
$startTimeNumber = (int) $meetings['dtStartZeit']; // "08:00:00" --> 8
To convert a string to an int you use intval()
because a string is an object of chars casting them wont ever work as expected which is what (int) is doing
I have two apps one written in php and one in python and both of them use the same mysql database.
For the public id of the entries in some of the tables I use binary(16) fields(I can't change this, it must remain this way).
The question is how does python does the conversion of this binary field?
Let's take one of the entries as an example.
When I get it in php(from the db) the value of the public id is °•WiCÄ‘õ0Iò|–g, the same value is shown in SequelPro. But php myAdmin does a hex function over binary fields and shows 0bb09557691443c491f53049f27c9667. Now I managed in php to convert the binary to the value showed in php myAdmin and it works for all the entries but I've just noticed that python does another conversion. When I get the entry used in this example via python the public id is owwweye1rjnvt3i1d0ib18x3.
What I need to achieve is to convert in php what I get from MySql: °•WiCÄ‘õ0Iò|–g to what python sees: owwweye1rjnvt3i1d0ib18x3. The php app makes calls on the python one(not developed by me) and thus the id needs to be in the same format for a successfull call.
Any suggestions are welcomed. Thanks.
EDIT: If i send °•WiCÄ‘õ0Iò|–g from php to python and print it rigth away I get: °•WiCÄ‘õ0Iò|–g
Finally I've sorted this out.
Seems that python converts to base36 not hex as I've wrongly supposed.
I've tried to simply base_convert 0bb09557691443c491f53049f27c9667 from 16 to 36 but I've got owwweye1rk04k4cskkw4s08s. Not really what I needed but still a great step further as it started to look like owwweye1rjnvt3i1d0ib18x3.
This difference I supposed to appear because of the large values to be converted(loss of precision), so I've further researched and found the bellow function, written by Clifford dot ct at gmail dot com on the php.net website:
<?php
function str_baseconvert($str, $frombase=10, $tobase=36) {
$str = trim($str);
if (intval($frombase) != 10) {
$len = strlen($str);
$q = 0;
for ($i=0; $i<$len; $i++) {
$r = base_convert($str[$i], $frombase, 10);
$q = bcadd(bcmul($q, $frombase), $r);
}
}
else $q = $str;
if (intval($tobase) != 10) {
$s = '';
while (bccomp($q, '0', 0) > 0) {
$r = intval(bcmod($q, $tobase));
$s = base_convert($r, 10, $tobase) . $s;
$q = bcdiv($q, $tobase, 0);
}
}
else $s = $q;
return $s;
}
?>
I don't think others will come across this issue very often, but still if it happens hope they'll find this instead of burning their brains out like I did :))))
I doubt if it is possible but I'm looking for the following:
E.g. $number's value is 1, can I get the next number, in this case 2, to be the value of another variable, e.g. $newnumber?
I prefer to do this in SQLite, so the numbers are stored in a database.
Try: $newnumber = ((int) ($number)) + 1, if this is for a primary key though just set the column to auto increment
$newnumber=$number+1;
I think it can't get more simpler than that.
Without knowing the larger scope of what you're trying to accomplish, my suggestion would be to use the increment operator in PHP.
Original answer:
Something like:
$number = 1;
$newNumber = $number++;
Correct answer (above gives wrong result):
$number = 1;
$number++;
$newNumber = $number;
From there you can do whatever you want with the second variable.
First I thought this was a stupid question, and i should do some search and it would be easy to solve. But I am afraid I just ain't getting anywhere!
The thing i need to do is simple. I have a U$ value and i want to divide it by 12. Thats it.
Well, the thing is that this value is outputed by a function, and echoes ok, look:
<?php
$preconormal = wpsc_the_product_price(); // it echoes like 99.90
$precoja = str_replace (".", "", $preconormal);
echo $precoja; //echo ok -> 9990
$quantas = '12';
$parcela = $precoja/$quantas; // ok, so divide 9990 by 12, right?
echo $parcela; //no!!!!! it echoes 0 :(
?>
I really hope you can help me!
You are trying to divide strings, if you used numbers say
$quantas = 12;
$precoja = 9990;
What happens?
It should fix the division, in which case, prior to the mathmematics, convert your vars to integs by
$quantas = intval($quantas);
$precoja = intval($precoja);
//your manipulation here..l
Remove the quotes...
$quantas = '12';
to
$quantas = 12;
$precoja = floatval($preconormal)*100;
$preconormal = $precoja / 12;
I'd change your 5th line by removing the single quotes and/or 6th line with $parcela = (int)$precoja / (int)$quantas; because as soon as you use the function str_replace then $precoja becomes a string. Also having the single quotes earlier on = '12' it is also a string and that division returns 0.
I am developing a joomla extension and in default.php file i have this code:
foreach($this->subv as $subv) {
$giorni = ((int)$subv->data_fine - (int)$subv->data_inizio);
$ore = ($giorni * 24) % 24;
echo $giorni.' : '.$ore;
}
$this->subv is an object that contains the result of a mysql query. My problem is that echo prints $subv->data_fine value, and not the result of the substraction. $subv->data_fine and $subv->data_inizio contain the result of time() function.
How can i resolve it this problem?
Thanks!
If I understand you problem correctly, $giorni is equal to $subv->data_fine, which would simply mean that (int)$subv->data_inizio evaluates to zero. Have you checked that?
You can use mysql to get the difference between 2 dates (DATEDIFF function):
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff