Jump to content

Recommended Posts

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>";

Link to comment
https://forums.phpfreaks.com/topic/64539-solved-date-calculations/
Share on other sites

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.

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.