Petty_Crim Posted August 12, 2007 Share Posted August 12, 2007 I'm having trouble with a simple date calculation. Basically I have to time stamps, $todays_date and $date_joined. I want to find out how long a member has been registered on my forum in days months years atm I'm taking $date_joined off $todays_date but its giving me an actual date which is 1971-06-05 but I actually want something like 30 days or 2 months 3 days not the actual date if that makes any sense. Heres my code: $todays_date=time(); $date_joined=$member_row['user_joined']; $join_time=$todays_date-$date_joined; echo "Join time: ". date('Y-m-d', $join_time)."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/64539-solved-date-calculations/ Share on other sites More sharing options...
NArc0t1c Posted August 12, 2007 Share Posted August 12, 2007 echo "Join time: ". date("Y-m-d", $join_time)." "; To echo "Join time: ". date("Y-m-d", $join_time)."; Try that. Quote Link to comment https://forums.phpfreaks.com/topic/64539-solved-date-calculations/#findComment-321707 Share on other sites More sharing options...
GingerRobot Posted August 12, 2007 Share Posted August 12, 2007 So you've stored a timestamp in the database for when they signed up? You'll be wanting to do something like: <?php $todays_date=time(); $date_joined=$member_row['user_joined']; $diff = $todays_date - $date_joined; $days = floor($diff/86400); echo 'You joined '.$days.' days ago'; ?> If its a date you've stored, you'll need to convert it to a unix timestamp with the strtotime() function. Quote Link to comment https://forums.phpfreaks.com/topic/64539-solved-date-calculations/#findComment-321708 Share on other sites More sharing options...
Petty_Crim Posted August 12, 2007 Author Share Posted August 12, 2007 So you've stored a timestamp in the database for when they signed up? You'll be wanting to do something like: <?php $todays_date=time(); $date_joined=$member_row['user_joined']; $diff = $todays_date - $date_joined; $days = floor($diff/86400); echo 'You joined '.$days.' days ago'; ?> If its a date you've stored, you'll need to convert it to a unix timestamp with the strtotime() function. thanks that worked perfectly. I'm curious though as to what floor and that 86400 number means. Also say I wanted to show how many months a member has been a member for, would I base a month on 30 days or 31 days? Whats the standard way? Quote Link to comment https://forums.phpfreaks.com/topic/64539-solved-date-calculations/#findComment-321722 Share on other sites More sharing options...
GingerRobot Posted August 12, 2007 Share Posted August 12, 2007 Well, the manual is your best friend for functions. floor() rounds a number down. The 86400 is the number of seconds in a day. Since we have a time difference in seconds, we divide by the number of seconds in a day to get the number of days difference. We round down so we show a whole number of days. Also say I wanted to show how many months a member has been a member for, would I base a month on 30 days or 31 days? Whats the standard way? That is exactly the problem. Id personally leave it as a number of days. Quote Link to comment https://forums.phpfreaks.com/topic/64539-solved-date-calculations/#findComment-321727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.