Hello! There is a task:
There is a site where you can select and grow crystals. For example, chose a crystal. They pressed the "grow" button. A value was recorded in the database - the time when the growth began. The crystals have a growth time (10 days for example). In order for a stone to grow faster, you can buy growth percentages. If we buy 1 percent growth - the start time is postponed a certain time ago. How can I find this time and how to find the percentage of readiness that will be after the start time is updated.
Perhaps there is another approach to find how much, as a percentage, the crystal grew after each page refresh and how much time was left for the crystal to grow.
I was helped to find a solution formula:
{время_старта} - ( (10*24*60) * 0.01) = новое время старта (10*24*60) / ({сейчас} - {новое время старта}) * 100 = текущий процент. Trying to perform operations with dates:
$новое_время_старта= date( 'Ymd h:i:s', strtotime($исходное_значение_времени_старта) - ( (10*24*60) * 0.01)); $date = date('Ymd H:i:s'); $текущий_процент = (10*24*60) / date( 'Ymd h:i:s', (strtotime($date) - strtotime($новое_время_старта)) * 100); If the new start time value is calculated, it seems to me, true, then with percentages the result is completely wrong.
Help please solve the problem.
#It seems he did everything as suggested by Sergiks, but the result is not the same ..
What we know now: $ days - the term of crystal growth in days
And so, they chose a crystal and pressed the button to grow. :
$time = time(); $start = new DateTime('@' . $time); $finish = new DateTime('@' . ($start + 86400 * $days)); $tsStart = date_timestamp_get($start); $tsFinish = date_timestamp_get($finish); $ tsStart and $ tsFinish - recorded in the database (in the form of int).
Further, we can see the current percentage of readiness with each page refresh. We think so:
$now = time(); $progress = (time() - $tsStart) / ($tsFinish - $tsStart); if( $progress >= 1) { // вырос, забирайте } else { // готовность 100 * $progress процентов } When buying interest ($ procent), we finish the finish time like this:
$finish = new DateTime('@' . ($tsStart + (1 - $procent / 100) * 86400 * $days)); And write to the database:
$tsFinish = date_timestamp_get($finish); And the percentage of readiness is also calculated:
$now = time(); $progress = (time() - $tsStart) / ($tsFinish - $tsStart); if( $progress >= 1) { // вырос, забирайте } else { // готовность 100 * $progress процентов } But the result is always different, there are even negative values. Tell me please, where was I wrong?