0

I've got the following bit of code on an intranet site that displays a different employee name every week - it cycles through the staff array. It basically updates the site with who is in charge of some maintenance on that particular week.

<?php  
$staff = array('John', 'Mike', 'Chris');
echo $staff[((time()+3*24*3600)/(7*24*3600)) % count($staff)];
?>

We've had to take the site offline for a while and we're directly accessing the page in our browser (not running through a web server).

Is there any way to adapt the code above to function in Javascript? I haven't used Javascript in ages and I figured it might be the best bet, but I'm a) not sure if it's possible and b) not sure how to go about it.

Any tips would be greatly appreciated!

3 Answers 3

5

This looks simple enough.

var staff = ['John', 'Mike', 'Chris'];
var selected = staff[ Math.floor(((Date.now()/1000) + 3*24*3600) / (7*24*3600)) % staff.length ];

console.log(selected);

NOTE: I use (Date.now()/1000) because time() returns seconds and Date.now() returns milliseconds.

NOTE 2: I use Math.floor as ((Date.now()/1000) + 3*24*3600) / (7*24*3600) might not return a whole number (and JavaScript returns different results than PHP when using % with decimals).

Sign up to request clarification or add additional context in comments.

2 Comments

Only thing you might want to watch out for is Date.now() compatibility with IE < 9.
@Radu: Didn't realize that was an issue... dammit IE.
1

Try this:

var timeInMilliseconds = new Date().getTime();
var timeInSeconds = Math.floor(timeInMilliseconds / 1000);

var staff = ["John","Mike","Chris"];
alert(staff[Math.floor((timeInSeconds + 3*24*3600)/(7*24*3600)) % staff.length]);​

Output

Chris

Notes: Added the Math.floor to force a whole number.

3 Comments

Use [] instead of new Array(), and replace document.write() with anything else
Odd. Every time I run it, I get a different output.
@user1572254 you were right, I was missing part of the formula, should be good to go now.
0

This was doing the rounds the other week and may be of interest: http://phpjs.hertzen.com/

4 Comments

Hmm... It looks promising, but when I try it I get: Notice: Undefined variable: false in /console.htm on line 1 I'm not sure if the formatting of my PHP code is off. It looks okay to me.
I've not used it myself yet. You could also have a look at phpjs.org to see if that gives you an alternative.
It's interesting, but hilariously excessive for something as simple as this. That there is a jackhammer and this problem is a nail.
I agree @Chuck but thought it was worth sharing. As for the issues that the OP ran into I think the problem lies in the (time()+3*24*3600)/(7*24*3600) % count($staff) equation resulting in a float

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.