简体   繁体   中英

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

我想创建一个为用户提供“虚拟硬币”的应用程序,每小时大约100个虚拟货币,如果用户注销,我如何继续以大约100个用户的每小时添加虚拟硬币?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM