PHP Match string with numbers and get next number (as Year) [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
These are my strings
2012-13
2013-14
2014-15
2015-16
2016-17
2017-18
I my string is 2014-15 as current financial year, I want my next my next financial year as 2015-16
How can I achieve this with PHP? I get these year list form mysql table, but I want next financial year in php

Try this:
$financial_year = "2012-13";
$fin_array = explode("-", $financial_year);
$next_fin_array[0] = $fin_array[0] + 1;
$next_fin_array[1] = $fin_array[1] + 1;
$next_financial_year = implode("-", $next_fin_array); // Gives 2013-14
Hope this helps.

$myStr = '2014-15';
$yr1 = (int) substr($myStr, 0, 4);
$yr2 = (int) substr($myStr, -2);
echo ($yr1+1).'-'.($yr2+1);
// 2015-16

$financeYear = DateTime::createFromFormat ( "Y-d" ,"2014-15");
$financeYear->modify('+ 1 year');
echo $financeYear->format("Y-d");
Result
2015-15

Related

Trying to get the most of the random element possibily from first 4 items in array php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi am having 10 elements in array . Am trying to get my random element mostly from first 5 elements. Which means random element appearance from first 5 elements should be much greater than next 5 elements
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = $arr[array_rand($arr)];
Am using above one to get the random element normally
Use function rand(min_num, max_num):
function rand5($array) {
$part = rand(1, 10);
return ($part > 3) ? $array[rand(0, 4)] : $array[rand(5, 9)];
}
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = rand5($arr);
Try this simple and easy code :-
$arr = array('a','b','c','d','e','f','g','h','i','j');
$rand = rand(0,9);
echo $arr[($rand <= 6 ? ($rand%5) : $rand)];
You just have to get the random number between 0 to 9 and divide that number in 70%(0-6) and 30%(7-9). If its greater than 5 then only use the remainder else directly get that number
Here is the fiddle :- https://3v4l.org/TWGJJ

How to convert the int value to inch in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I just want to know how to convert inch based on the integer inputted in the field.
For Example:
$int = $_POST['int']; //value 1
//computation here...
$inch = //formula
//echoing the inch
echo $inch;
If you want convert from centimeter can you use this source
function cm2inches($cm)
{
$inches = $cm/2.54;
$inches = $inches%12;
return sprintf('%d ins', $inches);
}
echo cm2inches(162);
You will have result
3 ins

how to decompose time in different part in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to decompose a time in two part, I mean by that, I want to take the left side and the right side, like a cut off right ?
22:02:00
23:00:00
23:12:00
Imagine those number, it doesn't matter if there's seconds or not, so we can kick them
22:02
23:00
23:12
Now, I to take the separate hour and minute. How can we do that ?
The simplest way would be to just cut out the first 5 characters of the string:
$time = '22:02:00';
echo substr($time, 0, 5); // 22:02
You can also parse the time using e.g. the DateTime class:
$time = '22:02:00';
$parsed = DateTime::createFromFormat('H:i:s', $time);
echo $parsed->format('H:i');
As Mark Baker commented, the strptime() function can also be used:
$time = '22:02:00';
$parsed = strptime($time, '%H:%M');
echo str_pad($parsed['tm_hour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($parsed['tm_min'], 2, '0', STR_PAD_LEFT);
Regular expressions would also work:
$time = '22:02:00';
preg_match('/^(?P<hour>\d{2}):(?P<minute>\d{2})/', $time, $result);
if (count($result) > 0) {
echo "{$result['hour']}:{$result['minute']}";
}

php- how to randomize first and last two digits? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know how I can randomize first and last two digits in php? Suppose the current code is:
FJA793HIYX
then after randomizing it should be:
HJA793HIGD
How can I do that?
Try
$str = 'HJA793HIGD';
$filter = substr($str,1,7);
echo $newstr= randLetter().$filter.randLetter().randLetter();
function randLetter() {
$int = rand(0,26);
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $alpha[$int];
return $rand_letter;
}
Try this!
$randomDigit = substr( "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,34 ) ,1 ) .substr( md5( time() ), 1)

Solve the equation in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to solve this equation in php:
Ym = Zm * Zm+1 * Zm+2...Zm+n
Where,
Y = Unknown
m = array if numbers (1,2,3,4...)
z = array of numbers (8,6,9,10...). Number of elements of "Z" and "Y" array dependent from array "m".
For instance:
m = (1,2,3,4);
z = (5,6,10,7);
Y1=5*6*10*7
Y2=6*10*7
Y3=10*7
Y4=7
This equation was written by me. I think I've done something wrong while writing equation (Ym = Zm * Zm+1 * Zm+2...Zm+n). What I need is the example above.
Thanks!
Something in the lines of:
$m = array(0, 1, 2, 3);
$z = array(5, 6, 10, 7);
$Y = array(1, 1, 1, 1);
for ($x=0; $x<count($m); $x++) {
for ($w=$x; $w<count($m); $w++) {
$Y[$m[$x]]=$Y[$m[$x]]*$z[$w];
}
print "Y[{$m[$x]}]={$Y[$m[$x]]}\n\r";
}
Example http://sandbox.onlinephpfunctions.com/code/063e363111f4da275475c9c39dd8e7281083d22b
In functional programming language like Mathematia aka Wolfram language this would just be:
Y=FoldList[Times,1,{5,6,10,7}]

Categories