Page 1 of 1

add 30 days to current date and minus by last login

Posted: Tue Nov 02, 2004 6:06 am
by fresh
hey, I am coding up a profiles page for my members.. and one policy my group and I like to follow is a monthly dump of inactive accounts, we determine this dump on one month of inactivity.. and that is based on the last login, which is recorded in the DB..

What I want to do is get the last login date (format: 11 01, 2004) and add 30 days to that:

Last Login: 11 01, 2004 + 30 days = 11 31, 2004

then I would like to capture the current date and first check to see if the time span between the last login and current date is greater than 30 days:

Current Date: 11 02, 2004 - 11 31, 2004 = 28 days

then if so, I would echo:

"This account has expired!"

.. otherwise I would echo:

"You have 28 days till this account expires..";


all help is much appreciated.. :D

Posted: Tue Nov 02, 2004 6:35 am
by Maugrim_The_Reaper
Convert both prior login date and current date to unix timestamps - you can minus from the other to come up with a difference in seconds - should be simple maths to convert from seconds to days, etc., within your code.

See the PHP manual for the relevant functions...:)

Posted: Tue Nov 02, 2004 11:10 am
by pickle
Ya, convert the last login to a timestamp, then use [php_man]strtotime[/php_man] to add a month:

Code: Select all

$converted_stamp = 9103897840; //<- made up number
$one_month_ahead = strtotime('+1 month', $converted_stamp);
That will, I believe, take into consideration months with different amounts of days.[/php_man]

Posted: Tue Nov 02, 2004 4:40 pm
by timvw
btw, almost every DBMS supports functions like [mysql_man]INTERVAL[/mysql_man]

Posted: Tue Nov 02, 2004 5:23 pm
by fresh

Code: Select all

<?php
echo strtotime("now"), " :: current date<br>";
echo strtotime("+1 month"), " :: one month past current date<br>";
?>

Ok after some research this is what I came up with as far as current date to unix time stamp and even to get 30 days from current date..

after a test here is my output:
1099438681 :: current date
1102030681 :: one month past current date
Does that look correct? How can I tell if thats the right dates? The difference between them two numbers is: 2592000

Also, how would I go about translating this format (11 01, 2004) to unix timestamp, because when I enter that date in place of now in my code, it comes back with -1 (an error)..

also, how would I minus the timestamps and return the amount of days between the two..

my main concern is pulling this date from the DB and trying to translate it in the format it's in now.. any help is much appreciated.. :D

Posted: Tue Nov 02, 2004 5:33 pm
by josh

Posted: Tue Nov 02, 2004 5:45 pm
by pickle
You can tell if it's the correct date by doing a big fancy date() call using that stamp and seeing what the output is.

As for getting the number of days, you could just subtract the latter stamp from the former stamp and divide by 86400 (the numbers of seconds in a day). That will give you an exact day count for 363/365 days. The other 2 are when Daylight Saving Time comes on and goes off, then you'll have to round a bit.

Posted: Tue Nov 02, 2004 5:58 pm
by fresh
who knew working with time would be so difficult.. :D

Code: Select all

<?php
$curdate = strtotime("now");
$expdate = strtotime("+1 month");
$mindate = $curdate - $expdate;
$divdate = $mindate / 86400;
$sumdate = $divdate;
echo "The amount of days between now and 1 month from now is :: ".$sumdate."";
?>
thats what I came up with, it seems to work fine.. I still wish I could see the real date.. or atleast the one I am use to seeing.. how are timestamps calculated??

thanks again for all the help :D

Posted: Tue Nov 02, 2004 6:18 pm
by pickle
Might I suggest calling [php_man]ceil[/php_man]() on $sumdate? That will make it an integer, rather than a decimal.

To see what I mean, set $cur_date to October 30th, 2004 and see what happens. $sumdate will likely be an decimal due to Daylight Saving Time.