PHP String modification Issue - php

I am having one weird problem.I am getting time of the day like 800 , 1200 ...
i want output String like 8:00,12:00..
I know alternatives but can anyone suggests me shortest way to complete it ..?plz

One way to do it:
echo preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);

Another way
echo substr($time, 0, 3 === strlen($time) ? 1 : 2) . ':' . substr($time, -2);
Or you can normalize the length
$time = str_pad($time, 4, 0, STR_PAD_LEFT);
echo substr($time, 0, 2) . ':' . substr($time, 2, 2);

if your source is string like "800" or "1200" then solution by deceze will do :
preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);
but if it's matter of date formatting then this is the right way to do it
date("H:i");

Related

How to convert variable from java to php?

I'm creating player panel for my friend in php, but I have problem with variable shown below. I must insert into variable in php, and send request to database. What is my problem? I can't convert java code, because I don't know how I can do this. It may be strange, but unfortunately it is.
I tried do this with amateur way, using;
require 'mojang-api.class.php';
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8); echo '-'; echo substr($uuid, 8, 12); echo '-'; substr($uuid, 13, 15); echo '-';
but, You and I know this - this way sucks.
I place the java code below.
Java:
uuid.substring(0, 8) + '-' + uuid.substring(8, 12) + '-' + uuid.substring(12, 16) + '-' + uuid.substring(16, 20) + '-' + uuid.substring(20)
If someone can help me with this problem, I'll be grateful.
Is this what you are looking for
$uuid = MojangAPI::getUuid('jeb_');
echo 'UUID: <b>' . $uuid . '</b><br>';
echo substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 13, 2);
Instead of plus + in Java you use the dot . in PHP. Amazingly you used it in the line above?

Number formatting using php

I am using php. I want to display a number in a '$XX.XXX.XX' format. I tried the following code, but its not giving full output.
<?php echo "$".number_format(round($customer['hourly_payment']),2); ?>
Output
$17,317.00
Please suggest a solution!
I believe you missed the following function's last 2 parameters:
string number_format ( float $number , int $decimals = 0 , string
$dec_point = "." , string $thousands_sep = "," )
For your case:
number_format($number, 2, '.', '.');
Read more: http://tr1.php.net/number_format
Try with -
echo number_format(100000000, 2, '.', '.');
Output
100.000.000.00

PHP number_format is rounding?

I have a price "0,10" or "00000,10"
Now when i try
number_format($price, 2, ',', '')
I get 0,00.
How can i fix this? I want 0,10 $.
I don't want rounding.
Or when i have 5,678, i get 5,68. But i want 5,67.
Several people have mentioned rounding it to 3 and then dropping the last character. This actually does not work. Say you have 2.9999 and round it to 3 it's 3.000.
This is still not accurate, the best solution is this:
$price = '5.678';
$dec = 2;
$price = number_format(floor($price*pow(10,$dec))/pow(10,$dec),$dec);
What this does is takes the price and multiplies it by 100 (10^decimal) which gives 567.8, then we use floor to get it to 567, and then we divide it back by 100 to get 5.67
You can increase the size of the number before rounding down with floor:
$price = floor($price * 100) / 100;
$formatted = number_format($price, 2, ',', '');
Another solution, which may give better precision since it avoids floating-point arithmetic, is to format it with three decimals and throw away the last digit after formatting:
$formatted = substr(number_format($price, 3, ',', ''), 0, -1);
you should convert comma-filled number back to normal decimal before with str_replace.
$number = str_replace(",", ".", $number);
and then you can use number_format
"00000,10" is a string. You should a decimal point. To get the desired behaviour, you could use:
echo substr(number_format(str_replace(',', '.', $price), 3, ',', ''), 0, -1);
Use this (needs activated intl PHP extension)
$numberFmtCurrency = new NumberFormatter('de_AT', NumberFormatter::CURRENCY);
$numberFmtCurrency->setAttribute(NumberFormatter::ROUNDING_INCREMENT, 0);
$numberFmtCurrency->formatCurrency(328.13, 'EUR'); // prints € 328.13 (and not 328.15)
If you are literally just wanting to clear leading zeroes and just limit the length, rather than round to a certain amount of decimal places, a more generalised solution could be this function:
function cutafter($string,$cutpoint,$length)
{
$temp = explode($cutpoint,$string);
$int = $temp[0];
$sub = $temp[1];
return number_format($int,0).','.substr($sub,0,$length);
}
Example:
$number = "005,678";
$answer = cutafter($number,",",2);
$answer now equals "5,67"
Just before number_format is executed the string "0,10" is converted by php to an number. because php always uses the engish notation the it won't look after the comma.
echo "4 apples" + 2;
output: 6
The " apples" part is ignored just as your ",10" is ignored.
Converting the "," to a "." allows php to see the other digits.
$price = str_replace(',', '.', '0,10');
number_format($price, 2, ',', '');
My problem was that html validator error messege thar number_format() argument is not double.
I solved this error message by placing floatval for that argument like number_format(floatval($var),2,'.',' ') and that is working good.
function format_numeric($value) {
if (is_numeric($value)) { // is number
if (strstr($value, ".")) { // is decimal
$tmp = explode(".", $value);
$int = empty($tmp[0]) ? '0' : $tmp[0];
$dec = $tmp[1];
$value = number_format($int, 0) . "." . $dec;
return $value;
}
$value = number_format($value);
return $value;
}
return $value; // is string
}
Unit Testing:
Passed / 1100000 => 1,100,000
Passed / ".9987" => .9987
Passed / 1100.22 => 1,100.22
Passed / 0.9987 => 0.9987
Passed / .9987 => 0.9987
Passed / 11 => 11
Passed / 11.1 => 11.1
Passed / 11.1111 => 11.1111
Passed / "abc" => "abc"
See this answer for more details.
function numberFormat($number, $decimals = 0, $decPoint = '.' , $thousandsSep = ',')
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
$number = $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
return number_format($number, $decimals, $decPoint, $thousandsSep);
}

Add a period to the middle string PHP

It will actually be a decimal but that is not the main point. I will have a set of numbers like:
8976
8765
3454
3453
10198
What I am wanting to do is add a decimal 2 places from the right. So the first would be 89.76 and so forth.
Can't you just multiply each by 0.01?
$formatted = number_format($unformatted_number / 100, 2, '.', '');
2 - decimal places
'.' - decimal separator
'' - thousands separator
docs for the function are here.
try this
$number = 8976;
$number = (float)$number/100;
results:
89.76
You may have to do some checking to see how many digits the number is, i.e 89768 would be devided by 1000 and so on.
Comments are available,
//the string you need to split
$string = "123456";
// read from right 2 character
$rightNums = substr($string, -2, 2);
// maximum 100 character to the left defined now
$otherNums = substr($string, -4, 100);
// pront them just with . between
echo $otherNums.".".$rightNums; ?>
hope it help much.
Try with this
$tmpString = substr("8976", 0, -2);
$finalString = str_replace($tmpString, "." . $tmpString, "8976");
echo $finalString;

printing with the penny value in php

I have these value stored in a decimal 10,2 field
1052730
956700
How do i print this using php so that the value is like
$10,527.30
$9,567.00
basically i am trying to avoid the value as
$1,052,730 <--- this i dont want
You can use the
money_format($format, $value)
function in php. The details of the formatting is given here.
Well, assuming that 1052730 is really 10527.30 as alluded to in your question:
$number = 1052730;
$decimals = $number % 100; //30 in this case
$digits = floor($number / 100);
$paddedDecimals = str_pad($digits, 2, '0', STR_PAD_LEFT);
$out = '$' . number_format($digits, 0).'.'.$paddedDecimals;
echo $out; // $10,527.30
There are no floating point calculations used for the decimal part, so there's no need to worry about precision issues (although at this precision it would likely be hard to get a float error in there)...
Just divide by 100:
<?php
echo number_format(1052730/100, 2, '.', ',') . PHP_EOL;
echo number_format(956700/100, 2, '.', ',') . PHP_EOL;
printf ("$%01.2f", ($input / 100));

Categories