0

How would you increase a a number on a webpage twice a week on specific days and times?

For example the webpage would read:

"2 Apples"

However every Tuesday & Thursday at 9:00pm the number should increase by two.

So by Friday the number should have increased to 6 "Apples"

What's a simple way to increment this in Javascript, php or Jquery?

N0xr-2
  • 25
  • 5
  • You may need a database or a file handling with this one – Ceeee Feb 12 '16 at 08:04
  • 1
    Save your number anywhere (database, text file..). Make a cron job and run a script which read, increase and save the number. – kosmos Feb 12 '16 at 08:04
  • As Kosmos pointed out, it can be done in many ways. As long as you store it centrally so all visitors see the same number of apples. So Javascript and jQuery would be less obvious, I wouldnt bother doing this in JS or jQuery. – Werner Feb 12 '16 at 08:11
  • Thanks all, I was thinking about this from the complete wrong direction. – N0xr-2 Feb 12 '16 at 08:12
  • I will post a calculation fo it in a few minutes. – AntiHeadshot Feb 12 '16 at 08:13
  • @Farang Finnaly ^^, added a JavaScript answer, where you can pass any date to get the amount of apples. Also you can change the days and time the updates will take place. – AntiHeadshot Feb 12 '16 at 09:35

2 Answers2

1

As it was mentionned in the comments, I advise you to use a cron to handle this.

First, you have to store your value somewhere (file, databse, ...). Then, you should create a cron job, that runs a code that increase and update your value at the given days of the week.

Some help about crons : https://en.wikipedia.org/wiki/Cron#Examples

I would add this post about cron and php : Executing a PHP script with a CRON Job

Hope it helps

Community
  • 1
  • 1
r4phG
  • 470
  • 8
  • 16
0

Here a Javascript version, you can pass it the date it should start counting, the date you want to know the ammount of apples of, the days of the week apples will be added, the hours of the day the updat will take place and the ammount of apples that will be added on each of these dates.

Date.prototype.addDays = function (days) {
    var result = new Date(this);
    result.setDate(result.getDate() + days);
    return result;
}
Date.prototype.addHours = function (hours) {
    var result = new Date(this);
    result.setHours(result.getHours() + hours);
    return result;
}

function getApples(startdate, date, updateDays, updateTime, applesPerUpdate) {
    var startDay = startdate.getDay();
    var firstUpdateDate;
    for(day of updateDays) {
        if (day >= startDay) {//assumes startdate has no time added
            firstUpdateDate = startdate.addDays(day - startDay).addHours(updateTime);
            break;
        }
    }
    if (!firstUpdateDate)
        firstUpdateDate = startdate.addDays(7 - (startDay - updateDays[0])).addHours(updateTime);

    var updateDaysReverse = updateDays.slice(0).reverse();//clones the array

    var dateDay = date.getDay();
    var lastUpdateDate;
    for(day of updateDaysReverse) {
        if (day < dateDay || day == dateDay && date.getHours() > updateTime) {
            lastUpdateDate = date.addDays(day - dateDay);
            break;
        }
    }
    if (!lastUpdateDate)
        lastUpdateDate = date.addDays(updateDaysReverse[0] - (7 + dateDay));
    lastUpdateDate = new Date(Date.UTC(1900 + lastUpdateDate.getYear(), lastUpdateDate.getMonth(), lastUpdateDate.getDate(), updateTime, 0, 0, 0));

    var secs = Math.trunc((lastUpdateDate - firstUpdateDate));
    if (secs < 0) return 0;

    var dayDiffs = [];
    for(day of updateDays)
        dayDiffs.push(day - updateDays[0]);

    var weeks = Math.trunc(secs / 604800000);
    var days = Math.trunc((secs % 604800000) / 86400000);
    var apples = weeks * updateDays.length;

    for(diff of dayDiffs)
    {
        if (diff <= days)
            apples++;
        else
            break;
    }
    return apples * applesPerUpdate;
}
// important, day and month is zero-based
var startDate = new Date(Date.UTC(2016, 01, 07, 0, 0, 0, 0));
var updateDays = [2, 4];// 0 = sunday , have to be in order and must be 0 <= x < 7
var updateTime = 9;

console.log(getApples(startDate, new Date(), updateDays, updateTime, 2));

Was more coplicated than I thaught, and i havn't testet it much, so there may be bugs.

Here is a plunker to play with the values.

AntiHeadshot
  • 1,130
  • 9
  • 24
  • wow, and here's me thinking it wasn't even possible. You're a legend, sir. – N0xr-2 Feb 12 '16 at 09:36
  • @Farang it needs a lot of date calculation which is annoying but doable. What you can't do with this method, is changing parameters and keeping the old result. You could calculate the old value up to the date you want to change, and then add the amount from that time to now with the new parameters, but it will get tedious. – AntiHeadshot Feb 12 '16 at 09:40
  • Is that really clean ? I mean, why would you do a repeatable task not using crons ? This works, but still you have to save your start date as well. I feel like it's way more complicated this way. You can do it in two lines with a server script – r4phG Feb 12 '16 at 11:01
  • with a file you have no chance of getting a value from the future or past, i posted this, because he was also asking for JS. – AntiHeadshot Feb 12 '16 at 11:06