Ryan K
Ryan K

Reputation: 346

How could i calculate numbers per hour when the user is not logged in, on PHP?

i want to create an application that gives the user "virtual coins" and roughly at 100 per hour, if the user logs out, how do i keep adding the virtual coins per hour at 100 per user roughly?

Upvotes: 0

Views: 244

Answers (2)

secelite
secelite

Reputation: 1373

You could do it with a cronjob that is executed every hour and calls a script that adds 100 coins to each user.

edit: If you can't or don't want to use a cj you can calculate it with the next login of each user. Just store the timestamp from last login and calculate it.

Here is an example with script:

assuming last login: 11-29-2012 00:00:00 timestamp: 1354143600

assuming new login: 11-30-2012 05:10:00 timestamp: 1354248600

php code:

<?php
  $t1 = 1354143600;
  $t2 = 1354248600;
  $diff = $t2-$t1;

  // calculate hours
  $hours = $diff/60/60;
  // coins
  $coins = $hours*100;

  print $hours . ' hours, ' . $coins . ' coins';

Outputs:

29.166666666667 hours, 2916.6666666667 coins

Upvotes: 3

Cryszon
Cryszon

Reputation: 387

You could store the time when user logs out. Then calculate the time elapsed and add coins when the user logs in again. If you need to add the coins at the exact same time for all users regardless of whether they're logged in or not, then I guess cron job might be better.

Upvotes: 0

Related Questions