1

I am developing game and I need to show to player, how much day he is under protection. This is what I have for now:

if(user::loged()){
  $protect = (60*60*24*8) - (time() - user::info['reg_date']);
  $left = date("n",$protect);
  if($left > 0) echo "You are protected for $left days!";
}

For first (test) user reg_date is 1394883070 (15.3.2014 11:31). So it should print

You are protected for 7 days!

But I get that

You are protected for 1 days!

Any ideas?

Patrik Krehák
  • 2,595
  • 8
  • 32
  • 62
  • 2
    It has 1394883070, from database (I wrote it) – Patrik Krehák Mar 16 '14 at 18:38
  • You're better off using Date Diff. I'm on my phone so I can't give much info. But here's the doc URL: http://us2.php.net/manual/en/datetime.diff.php – Kyle Mar 16 '14 at 18:40
  • 1
    possible duplicate of [PHP: get number of days between two dates in format YYYY-MM-DD](http://stackoverflow.com/questions/3185168/php-get-number-of-days-between-two-dates-in-format-yyyy-mm-dd) – Amal Murali Mar 16 '14 at 18:44

3 Answers3

2

You should do something like this:

$days_since_registration = (time() - user::info['reg_date'])/(24*3600)

date() is useful for only unix timestamps. The difference of timestamps is a time interval in seconds, if you use it as a timestamp you are using dates in 1970 or something similar happens.

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
1

You have set $left to the number of months.

n is Numeric representation of a month, without leading zeros - http://php.net/date

I would do

if(user::loged()){
  $protect = 691200 - (time() - user::info['reg_date']);
  $left = ceil($protect / 86400);
  if($left > 0) echo "You are protected for $left days!";
}
Barry Carlyon
  • 1,039
  • 9
  • 13
0
<?php
   $protect = (60*60*24*8) - (time() - user::info['reg_date']);
   $left = ltrim(date("d",$protect), 0);
   if($left > 0) echo "You are protected for $left days!";
   // Prints "You are protected for 7 days!"
?>
makallio85
  • 1,366
  • 2
  • 14
  • 29