Duration to min - php

I make some search on Google and Stackoverflow but does not reach anything
I use template lite and retrieve movie durations via it. In my db, the durations are like that
0215
0310
Is there a way in template lite or php to show them in mins ?
ex:
0215 = 135

$duration = "0215";
$mins = substr($duration,0,2)*60+substr($duration,2);
Simple enough.

function toMinutes($duration) {
return ltrim(substr($duration, 0, 2), '0') * 60
+ ltrim(substr($duration, 2), '0');
}
The trimming is to avoid the string being cast to an octal instead of an integer, just in case.

Related

How to add numeric value to value without adding or subtracting it in php

I would like to post a value to my MySQL database in this format;
01, 02, 03...
011, 012, 013...
0101, 0102, 0103,
etc.
(with a 0 before each value).
If I do it like this "01+1" I (understandable) get the value 2 and not "02".
is there a default php function that let's me set the default amount of characters or is there an other option?
I'm using laravel combined with eloquent and some own functions like so;
$lastSKU = $data->GetData('get', 'products', 'filter')->max('SKU');
$newValue = $lastSKU+1;
Thanks in advance.
It sounds like you won't know how the format will be, if there is always a 0 in front it is easy enough to do
$value = '0'. ( $value + 1 );
PHP will automatically convert it to a number between the brackets and by adding a string to it it will become a string again.
Now if you do not know the the length or the padding in advance but you do need to keep the current padding I would suggest something like
$value = str_pad(($value + 1), strlen($value), '0', STR_PAD_LEFT);
Edit, str_pad is a bit slow these days, sprintf can do the same trick but much faster
$value = sprintf("%'.0" . strlen($value) . "d\n", ($value +1));

2 decimal places output

My apologies if this was discussed before, but based on my study and observation from different discussions all of them doesn't work for me. I had a formula that computes a certain percentage. My problem is, it must be only in 2 decimals. It shows like 12.1212121212% rather than 12.12%
Here's my formula and by the way I'm using php code.
$p = $c/$t * 100
You can use number_format() in php
$p = number_format((float)( $c/$t * 100), 2, '.', '');
In JavaScript you can use toFixed(2)
var $c = 1,
$t = 1333,
$p = ($c / $t * 100).toFixed(2);
document.write($p);

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.

Unix timestamp to LDAP timestamp

There's a lot of examples on how to convert LDAP->Unix, but I can't for the love of god convert it back as in Unix->LDAP..
Here's what i've got for LDAP->Unix:
How to convert LDAP timestamp to Unix timestamp
http://www.morecavalier.com/index.php?whom=Apps%2FLDAP+timestamp+converter
function LDAPtoUnix($t) {
$secsAfterADepoch = $t / (100000000);
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;
return intval($secsAfterADepoch-$AD2Unix);
}
Which i think should be accurate.
But I'm twisting my puny little brain to reverse the mathematics and i can't figure out it..
My head is boiling to just calculate the difference in seconds between the different epochs and simple adding/subtracting them to given time parameter?
Can someone shed some light on how to reverse the timestamp?
Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms679430(v=vs.85).aspx
Also the main reason for why I'm asking besides my brain not wanting to compute that mathematics is that the floating point mechanism of PHP appears to be not so specific as it needs to be?
If i calculate Unix->LDAP timestamp, i'll end up with 1.3009518089E+17 and i'm not sure that Active Directory likes this particular notaion, so i would need to store it in a string but i can't figure out how to calculate these large numbers and not end up with a double.
At the moment i've got:
printf("%.0f", UnixToLDAP(time()));
which gives me the correct length but it's not really specific.
What i need
Short and simple,
Unix->LDAP timestamp that fits in pwdLastSet in Active Directory.
Also, it must be as perfect as possible, my attempts doesn't end well when checking: http://www.morecavalier.com/index.php?whom=Apps%2FLDAP+timestamp+converter
Google: Automatic solution (Windows 2012 Active Directory)
You can simply state -1 in pwdLastSet and Windows will automaticly set the time to the last login after the password was changed. It's not perfect but it works for anyone googling and ending up here.
You can make your function a bit more readable by naming the input parameter properly and assigning an intermediate variable before returning. Since $AD2Unix is actually a fixed term, it could probably be a constant; for now, I've just moved it to the top of the function:
function LDAPtoUnix($ldap_ts) {
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;
$secsAfterADepoch = $ldap_ts / 100000000;
$unix_ts = intval( $secsAfterADepoch - $AD2Unix );
return $unix_ts;
}
We now have 2 lines to invert; they clearly need to happen in reverse order, and then using simple algebra to rearrange the terms in each (/ becomes *, - becomes +) we get this:
function UnixtoLDAP($unix_ts) {
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;
$secsAfterADepoch = intval( $AD2Unix + $unix_ts );
$ldap_ts = $secsAfterADepoch * 100000000;
return $ldap_ts;
}
A couple of tests suggest this inverts the original nicely.
Don't use intval(), it will overflow on 32-bit PHP, just let PHP use float.
function toLdap($unixTimeStamp) {
return ($unixTimeStamp + 11644473600) * 10000000;
}
function toUnix($ldapTimeStamp) {
return (int) (($ldapTimeStamp / 10000000) - 11644473600);
}
And you can calculate the diff of Unix and LDAP time using DateTime (more readable)
$diff = (new DateTime('1970-01-01'))
->diff(new DateTime('1601-01-01'))
->days * (24 * 60 * 60);
echo $diff; // 11644473600
Something like this?
function UnixToLDAP($t) {
$AD2Unix = ( (1970-1601) * 365 -3 + round((1970-1601)/4) ) * 86400;
return intval($t+$AD2Unix) * 100000000;
}

Convert string to integer and multiply two integers

I have a big time trying to either convert a string into a integer or multiply two integers. I can't convert the string into integer because it's resulting me into a boolean (when I'm using var_dump). I can convert the other integer in string, but I'm unable to multiply it.
I have this:
<? $fees=$commerce->cart->get_total();
$payfee = str_replace(' €', '', $fees);
$payfee = str_replace(',','', $payfee); //this is the string
$fee = 0.025;
$paypal = $payfee * $fee; //this thing is not working
?>
I tried converting the payfee in integer, but still can't make it work. I did something like this before and worked well, but not this time.
Any help will be appreciated.
P.S Thank you to the whole stackoverflow.com community which helped me many times before.
OP is running WooCommerce, and his $commerce->cart->get_total();
function responds output such as <span class="amount">560 €</span> (560 €)
and he's asking how to convert this to a number so he can get a fee
(2.5 %) from the amount.
First of all, the problem here is that the get_total() function responds with a string.
The correct way to fix this string would be a simple example such as
<?php
$totalAmountString = $commerce->cart->get_total(); //<span class="amount">560 €</span>
$totalAmountString = strip_tags($totalAmountString); //get rid of the span - we're left with "560 €"
$totalAmountString = str_replace(array(" €", ","), "", $totalAmountString);
$totalAmountFloat = (float)$totalAmountString;
$fee = 0.025;
$feeForThisAmount = $totalAmountFloat * $fee;
var_dump($feeForThisAmount);
$totalAmountWithFee = $totalAmountFloat + $feeForThisAmount;
var_dump($totalAmountWithFee);
?>
However, according to the Woo Commerce API Documentation you should be able to use $commerce->cart->total to get a float of the number, so a solution that might also work (again, I know nothing about WooCommerce), would be the following:
<?php
$totalAmountFloat = $commerce->cart->total;
$fee = 0.025;
$feeForThisAmount = $totalAmountFloat * $fee;
var_dump($feeForThisAmount);
$totalAmountWithFee = $totalAmountFloat + $feeForThisAmount;
var_dump($totalAmountWithFee);
?>
Edit
According to your latest data dump, the problem is that you're using
$paypal_fees=$woocommerce->cart->get_total() * 0.025;
where you should be using
$paypal_fees=$woocommerce->cart->total * 0.025;
as ->get_total() receives a string, and ->total receives a float.
try this
$integer =(int)$string;
LIve example
with var_dump() its correct
check this link
Use type casting like
$integer = (int)$myString;
then you can convert it to an integer,and its become easy to multiply
Use intval() function to covert string to integer
intval
From your elementary school technique based algorithm is posted here . You need not to convert string to integer in this case

Categories