This question already has answers here:
MySQL custom primary key generator
(3 answers)
Closed 3 years ago.
I have a pattern of integer (0000012019). where 2019 is current year and 000001 need to increase with every new row inserted to db like 000002, 000003, 000004, 000005 and so on.
how can i achieve this?
First of all it's a string you have.
You can split the string with substr() and do the math on the first part and then convert back to string with str_pad by padding with 0's from left.
$str = '0000012019';
$first = substr($str, 0, 6);
$year = substr($str, -4);
$first = str_pad($first+1, 6, 0, STR_PAD_LEFT);
$new = $first . $year;
echo $new; // 0000022019
Related
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 8 months ago.
Is there a one line way to combine getting first and last character of a string?
I tried (without success):
$title = "IncredibleTitle";
$title_characters = (mb_substr($title, 0, 1, 'UTF8')) && (mb_substr($title, 0, -1, 'UTF8'));
Use . period character for concatenation.
To get last character, 2nd param has to be -1 and third param will be value 1 or the length. See mb_substr.
Snippet:
<?php
$title = "IncredibleTitle";
$title_characters = (mb_substr($title, 0, 1, 'UTF8')) . (mb_substr($title, -1, 1, 'UTF8'));
echo $title_characters;
Online Demo
This question already has answers here:
How to make number_format() not to round numbers up
(18 answers)
Closed 3 years ago.
I have this number
$sku = '2200081005966';
and I want to convert the number like this without rounding the number
$new_sku = '5.96'
I already try this number_format(substr($sku, 7, 12), 0, '', '.')
but the output that I get 5.97.
Any ideas how can I make this work?
Thank you.
Add floor() around your number_format:
$sku = '2200081005966';
echo floor(number_format(substr($sku, 7, 12), 0, '', '.')*100)/100;
Outputs:
5.96
Note: It would works well in case of positive numbers, your substr always take a positive number, that's why it would be enough.
<?php
$sku = '2200081005966';
$foo=substr($sku, 9, 12);
echo substr($foo, 0,2).".".substr($foo, 3,3);
?>
first you get the last 4 charakters of the string, then you split that part and put a dot between.
This question already has answers here:
How do I convert output of number_format back to numbers in PHP?
(12 answers)
Closed 1 year ago.
I used number_format in a number. I want to know how to convert number-formatted variable in php back to its original format
I first used number_format. And I want to echo back its original format removing the number format.
$number = 500000
echo number_format($number, 2);
$number = 500000;
$formattedNumber = number_format($number, 2);
$getBackOriginl = explode('.',$formattedNumber);
echo str_replace(',','',$getBackOriginl[0]);
This question already has answers here:
How do you pull first 100 characters of a string in PHP
(6 answers)
Closed 10 months ago.
using PHP, How can i remove the rest of character if sting_Len is greater than 6 for example i have 600275L and i want to end up like 600275 but only if greater than 6 digit.
i used the following code to to extract value starts with 600, i want update it to work for the above condition, Thank you
if((substr($key, 0, 3) == "600") && ($row['ItemClass']==3))
{
$assy = $key;
$rout = "Assy";
}
If you always want to limit it to six characters, then you should just be able to use substr for this without checking the length. If you write:
$string = 'abcdefg';
$string = substr($string, 0, 6);
then $string will equal 'abcdef'.
But if $string is shorter than 6 characters, it will just return the entire string. So if you write:
$string = 'abc';
$string = substr($string, 0, 6);
then $string will equal 'abc'.
You can see this in the PHP manual here.
Use the following logic
if(strlen($code) > 6) { echo substr($code, 0, 6); }
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?
Example:
$year = 11;
$month = 4;
$stamp = $year.add_single_zero_if_needed($month); // Imaginary function
echo $stamp; // 1104
You can use sprintf: http://php.net/manual/en/function.sprintf.php
<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>
It will only add the zero if it's less than the required number of characters.
Edit: As pointed out by #FelipeAls:
When working with numbers, you should use %d (rather than %s), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.
For example:
sprintf("%04s", 10); returns 0010
sprintf("%04s", -10); returns 0-10
Where as:
sprintf("%04d", 10); returns 0010
sprintf("%04d", -10); returns -010
You can use str_pad for adding 0's
str_pad($month, 2, '0', STR_PAD_LEFT);
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
The universal tool for string formatting, sprintf:
$stamp = sprintf('%s%02s', $year, $month);
http://php.net/manual/en/function.sprintf.php