add 30 days to current date and minus by last login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

add 30 days to current date and minus by last login

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...:)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

btw, almost every DBMS supports functions like [mysql_man]INTERVAL[/mysql_man]
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post 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
Last edited by fresh on Tue Nov 02, 2004 5:44 pm, edited 2 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply