This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 8 months ago.
I've date in following format.
2019-02-27
I've to find sum of digits in above date. I've exploded the above value like this,
$date = '2019-02-27';
$dateArray = explode('-',$date);
Now date array will be like this:
array:3 [▼
0 => "2019"
1 => "01"
2 => "02"
]
With 2 loops, I can get sum of all digits but if there any much better way or builtin function to do this ?
Any kind of suggestion or help is appreciated.
You can use str_split to split the date into separate characters and then array_sum them. array_sum will ignore the - characters which are not valid numbers:
$date = '2019-01-02';
echo array_sum(str_split($date));
Output
15
Note that for your $date string of '2019-02-27' the correct result is 23. 15 is the correct result for '2019-01-02' which is what your var_dump output says was the contents of your $date variable at the time.
With 2 loops, I can get sum of all digits but if there any much better way or builtin function to do this ?
Yes
$total = array_sum(preg_split('//','2019-03-17'));
echo $total;
Output
23
Sandbox
Related
This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed last year.
I am trying to get the value between 001 to 012 with range function but it's returning 1,2,3....12
range(001,012)
but i want value like this 001,002,003,,,012
Need to loop through and append them:
foreach (range(1, 12) as $number){
$numbers[] = sprintf("%03d", $number);
}
print_r($numbers);
I get POST request date strings in the format of mY (no leading zeros on the month), so there's no delimiting character to split on.
Examples: 122019 or 62019
I need to separate the five or six digit string into a one or two digit month and a four digit year.
Eg1 : 122019
$a[0] = 12;
$a[1] = 2019
Eg2 : 62021
$a[0] = 6;
$a[1] = 2021
I don't know about this date format, especially about how you use an integer as a date. But let's consider that it's on purpose, the year is always gonna be 4 character, so you can just get the year by taking the last 4 char and use the rest as the month.
Using substr() see PHP.NET Substr
Return part of a string
And you can specify the start and length of the part you want to get, or using - to get character starting from the end of the string.
$weirdDate = 122019;
//takes the last 4 character
$year = substr($weirdDate , -4);
//takes the string from the beginning to 4 char before the end
$month = substr($weirdDate , 0,strlen($weirdDate)-4);
echo $year;
echo $month;
Again, it seems like a weird way to get a month/year date, but i'm answering based on the assumption that a year is gonna be 4 char long.
If it's not, you can't really split the number since the month part can be 1 or 2 char long.
09-2019 would be 92019
11-2019 would be 112019
A simple use of the substr() function will do this nicely
$in = '122019';
$year = substr($in,-4);
$month = substr($in,0, strlen($in)-4);
echo $year . ' month ' . $month;
$a[] = $month;
$a[] = $year;
RESULT
2019 month 12
array (
[0] => 12
[1] => 2019
)
Or if we use $in = '62019';
The RESULT would be
2019 month 6
array (
[0] => 6
[1] => 2019
)
Reference 'substr()`
You could also use substr with strpos. With substr(), you could first get the year by providing a negative offset to start capturing from back of the string as the year is going to be 4 digits. Then, you could use strpos() to find the index of the year and use this as the ending index to get the month.
That being said, best way to deal with this data is to either have a proper date format or better to have a JSON string with proper keys for days, month and year along with date.
Code:
<?php
$str = '122019';
$year = substr($str,-4);
$month = substr($str,0,strpos($str,$year));
echo $month," ",$year;
I have one another solution to use str_replace() with substr() like:
<?php
$string = "122019";
$year = substr($string, -4);
$date = str_replace($year, "", $string);
$myArray = array($date,$year); // convert into an array
echo "<pre>";
print_r($myArray);
?>
Desired Output:
Array
(
[0] => 12
[1] => 2019
)
Side Note: This will only work, if your year based on last 4 characters and other then these 4 characters must be date, as you mentioned in your question.
To accomplish this feat with two function calls, use negative parameters with substr() calls.
There is no reason to call strlen(), strpos(), or str_replace().
For a single call technique, use preg_split() with a lookahead pattern to ensure that no characters are consumed while exploding.
Codes: (Demo)
$mY = '62021';
var_export([
substr($mY, 0, -4), // month
substr($mY, -4) // year
]);
echo "\n---\n";
var_export(
preg_split('/(?=\d{4}$)/', $mY)
);
This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);
Im looking for someone to point me in the right direction to coding some statistical comparisons. Currently I query my database, and will get data back as follows:
main data set :
3,4,7,10,5,8,1,3,7
sets to compare with could be like this, and can have multiple sets.
4,5,6,9,10,2,3,4,6
Now i need to work out the difference between these two sets of data - for example, difference between 3-4 is 1. I then need to choose the biggest difference, the most agreed upon, and the lowest scoring.
How would you tackle coding this?
I would recommend to use the function array_walk(), passing the first array as the first parameter and the second array as the third, optional parameter. It would look like this:
<?php
$array_1 = array(3,4,7,10,5,8,1,3,7);
$array_2 = array(4,5,6,9,10,2,3,4,6);
// making a copy, because the callback function
// works on the actual value
$array_1_copy = $array_1;
echo '<pre>';
array_walk($array_1_copy, 'difference', $array_2);
echo "\nThe maximum difference is: ". max($array_1_copy);
echo "\nThe minimum difference is: ". min($array_1_copy);
echo '</pre>';
// the callback function, takes the 1st param as reference
function difference(&$value_1, $index_1, $array_2) {
$difference = abs($value_1 - $array_2[$index_1]);
echo "Difference between $value_1 and {$array_2[$index_1]} is $difference\n";
$value_1 = $difference;
}
?>
And the output for this code is:
Difference between 3 and 4 is 1
Difference between 4 and 5 is 1
Difference between 7 and 6 is 1
Difference between 10 and 9 is 1
Difference between 5 and 10 is 5
Difference between 8 and 2 is 6
Difference between 1 and 3 is 2
Difference between 3 and 4 is 1
Difference between 7 and 6 is 1
The maximum difference is: 6
The minimum difference is: 1
$max_diff = 0;
for ($i = 0; $i < (min(count($array1), count($array2));$i++){
if (($array1[$i]-$array2[$i]) > $max_diff ) $max_diff = $array1[$i]-$array2[$i];
}
echo $max_diff;
Something like that...Didn't actually tested it, but that's the idea.
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);