Using the function that separates a string from an array, we will write a program that returns our date of birth. We will also use an array named “years” which will have 12 random numbers between 2004-2021. We will search within the “years” array for the number that is located in the position of our month of birth, and we will subtract this number from our date of birth. This will return how old we were in that year.
For example:
In this date of birth: 2/17/1980
2 – is the month of birth
1980 – is the year of birth
The years array contains: 2004, 2020, 2014, 2018, 2016,…
In this example, our function will look for position 2 in the array (since 2 is the month of birth we input in the function). In the Array, we see that 2014 is in position 2. We will subtract this value from our date of birth that was input. The output result in this example be:
Random year selected: 2014
Your age in that year: 34
************************************
From the above, I understand that we must use an explode() function with an array that has 12 random numbers from 2004-2021. We will insert our date of birth in the function, and using the month’s numerical position in the array, the function will identify which one was selected. Then, it will subtract our year of birth from the year that was selected in the array and return it in the browser view like this:
Random year selected: 2014
Your age in that year: 34
I have uploaded an example of what we did today


0 comments