S.Stevenson S.Stevenson - 3 months ago 10
PHP Question

PHP - How to iteratively reduce a variable each week at a certain time?

Can anyone offer suggestions on how to reduce a value by a certain amount on a weekly basis, say Sunday at midnight in the local time (neither I nor the server are in the local time zone)?.

For example, the displayed value might be 1000, and I need it to reduce by 100 every week from midnight on each respective Sunday at midnight (so that in week 2 the displayed value is 900, the week after 800 and so on).

Can anyone offer any suggestions on how to go about that?

Thank you for your time.

Answer
$amtToReduce = 100;
$todaysDate = new DateTime("now");
$startDate = new DateTime("2016-08-21"); //Sunday's date
$getDiff = $startDate->diff($todaysDate);
$numDays = (int)$getDiff->days; //returns number of days between two dates
$productPrc = $actualPrice = 1000;
if($numDays%7==0) { //condition to allow only sundays
   $toInc = $numDays/7;
   $toReduce = $toInc*$amtToReduce;
   $productPrc = $actualPrice-$toReduce;
   //alter any condition here, to stop incrementing reducing price
}

echo $producPrc; //prints 100 RS less on each incrementing week end

Check the above piece of code, hope this could be helpful.

Unable to format my code on mobile, hope someone would edit and format it, surely appreciable.

Comments