0

我正在尝试制作一个脚本,在时间戳之后每天对用户进行惩罚。第一天扣1分,第二天扣2分,第三天扣4分,第四天扣8分,16分,32分,64分,以此类推。

我将如何自动生成 strtotime 和乘法?

我什至不知道此时我正在寻找什么,这使得搜索变得困难,抱歉,如果这已经发布。

4

2 回答 2

0

试试这个

$initial_date     = '2012-10-20';
$initial_datetime = new DateTime($initial_date);
$today_datetime   = new DateTime();
$days_passed      = $initial_datetime->diff($today_datetime)->format('%a');
$penalty          = pow(2, ($days_passed-1));
echo $penalty;
于 2012-10-25T06:08:10.423 回答
0

假设 $timestamp1 是第一天的时间戳,而 $timestamp2 现在是时间戳。然后:

$difference = abs(strtotime($timestamp2)-strtotime($timestamp1));

$days = floor($difference / (60*60*24));
$penalty = pow(2,$days);

echo "{$days} left, so your penalty is: {$penalty}";

样本结果:

0 left, so your penalty is: 1
1 left, so your penalty is: 2
2 left, so your penalty is: 4
3 left, so your penalty is: 8
4 left, so your penalty is: 16
5 left, so your penalty is: 32
6 left, so your penalty is: 64
...
于 2012-10-25T06:08:18.360 回答