Subtraction with php and mysql - php

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

Related

PHP - Strange result when compare 2 float values

I am trying to compare 2 values in PHP.
My logic is:
I have a remaining amount (a)
I have a amount to be charged (b)
I calculate remaining to be by ( a - b )
After charge action I get the actual remaining value (c)
I compare the value I got in #3 with (c)
Even though the both are similar PHP says they are not equal.
Below given is my code (with filled values)
<?php
$remaining_amount_before_payment = "600";
$remaining_amount_after_payment = (float)$remaining_amount_before_payment - (float)"387.60";
$actual_remaining_amount_after_payment = "212.4";
echo "actual_remaining_amount_after_payment: {$actual_remaining_amount_after_payment} <br><br>";
echo "remaining_amount_after_payment: {$remaining_amount_after_payment} <br><br>";
var_dump( ((float)$actual_remaining_amount_after_payment) == ((float)$remaining_amount_after_payment) );?>
I type cast the values to float, but the var_dump returns FALSE.
Can anybody help me to find out why this is?
I am using PHP 5.6.
Thanks in advance!
Bingo!
After several attempts I caught the catch. I was going crazy.
The "problem" is inside the right rounding values
$remaining_amount_before_payment = floatval("600"); // use floatval istead of (float)
$remaining_amount_after_payment = round($remaining_amount_before_payment - floatval("387.60"), 2);// use floatval istead of (float) and round result
$actual_remaining_amount_after_payment = floatval("212.4");// use floatval
echo "actual_remaining_amount_after_payment: {$actual_remaining_amount_after_payment} <br><br>";
echo "remaining_amount_after_payment: {$remaining_amount_after_payment} <br><br>";
var_dump( $actual_remaining_amount_after_payment === $remaining_amount_after_payment ); // return TRUE
Example
VoilĂ !
Use var_dump(abs(floatval($actual_remaining_amount_after_payment) == floatval($remaining_amount_after_payment)) == 0);
acual your variable '$remaining_amount_after_payment' is not realy 212.4
use a var_export to determine its value.
In my concern, You should "round" your floats values to a precision. round(x, precision) for comparison

PHP - How can I display time correctly?

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]";
}

similar_text - string / integer comparing

it's my first post here, so welcome everyone!
I'm trying to write a rule to simply protect my website against flooding by users posting it's content. I decided to use similar_text() function in PHP to compare strings (last added string by user and the one that one is adding at the moment), calculate similarity (%) and if the result is too high (similar in more than 90%) the script will not add a record to database.
Here is what I have:
similar_text($last_record, $new_record, $sim);
$similarity = (int) number_format($sim, 0);
if ($similarity < 90)
{
// add the record
}
else
{
// dont add anything
}
The problem is with this: if ($similarity < 90). I format the number and then convert it from string to int value, but the script doesn't care.
When I use quotas it works: if ($similarity < "90"). The question is why script doesn't work when I use the value as an integer value and it works when I use it as a string?
number_format returns a string where you need an int. So a string comparison to "90" works, but an int comparison fails. You can use int_val to convert to an int.
Also, I'm wondering if maybe you have something else wrong. I took your code sample and ran it locally, and it seems to work just fine even without swapping (int) for int_val.
With the following values:
$last_record = "asdfasdfasdf";
$new_record = "aasdfasdfasdf";
$similarity is 96 and the greater than section of the if triggers.
With these values:
$last_record = "asdfasdfasdf";
$new_record = "fffdfasdfasdf";
$similarity is 80 and the less than section of the if triggers.

Comparing datetimes in php not working

I have the following session value
$_SESSION["time"] = 2015-01-09 23:57:38 (example value)
And the variable $test
$test = time() - (60 * 1); (1 minute)
$test = date("Y-m-d H:i:s",$test);
What I want to do is check whether 1 minute as passed. To do this I have the following code:
if(isset($_SESSION["time"]) && (strtotime($_SESSION["time"]) > $test)){
echo "first";
}else{
echo "second";
}
Independently to which logic comparison I use (< or >), after a minute a page refresh still echoes the same... any idea on why? I'm finding this really strange...
Comparision should be like
($_SESSION["time"] > $test)
PHP now will compare two string values instead of integer (return value from strtotime call) and string (return value from date call)
What he actually wants is...
strtotime($_SESSION["time"]) > strtotime($test))

PHP variable not incremented

I have a small code as part of a huge file as follows:
if(($lastLogTime + $logOffset)>= $text1)
{
echo $text1.'<br>';
$uptime=$uptime + (($text1 - $lastLogTime)/60000);
echo ($text1 - $lastLogTime).'<br>';
fwrite($fd, $uptime.',');
echo $uptime.'<br><br>';
$lastLogTime = ($lastLogTime + 1800000);
echo $lastLogTime.' ME <br>';
}
The weird part is the output for the final $lastLogTime is NOT getting added by the 1800000 OR a variable called $logInterval = 1800000 which was initialized earlier.
The output is
1298083876650 - i.e lastLogtime
1298083877661 - text1
1011 - the difference
0.01685 - uptime
1298085676650 ME - damn ! doesn't get added by 1800000
NEW EDIT :
I solved it ! bad answers guys.. Thanks for the time anyways.
Am i the only one facing weird shhit like this ?
I suspect it has something to do with this:
What's the maximum size for an int in PHP?
Check out the php date function. Though working with dates can be tedious, converting everything to seconds normally isn't the best approach.
http://php.net/manual/en/function.date.php

Categories