Jump to content

[SOLVED] php/mysql date manipulation (for annual membership periods)


brett24

Recommended Posts

I've been on a seemingly simple task for two days now.  I'm hoping someone here can give me a hand.  I'm trying to write code to do the following.  (I've always had a bit of a mental block on the date and time functions with php.)

Insert into a mysql table today's date
//this I'm thinking I can do by setting $var=now()

Also insert today's date plus one year (to show the end date of the annual membership period).
//pulling my hair on how to add a year

Also, if the member renews the membership I need to be able to add another year to membership period (without losing the time left on the current annual period - so if they have a month left, the new end date would be 13 months into the future after adding another year).

Thanks for any help.
-Brett
[quote author=jesirose link=topic=124348.msg515033#msg515033 date=1169941078]
today:
$sql = "INSERT INTO table(field) VALUES(now())"; Don't use a variable, just do now()

plus a year
$year = time()+(60*60*24*365);
[/quote]

Thanks.  I'll do the first part.  For the second part I was hoping to find a '+1 year' sort of solution that takes care of leap years too.  I then need to be able to call the value from mysql (the end date of the membership) and add a year at any time.  Any thoughts on that?

-Brett
time() is an ok solution if you are always going a year from now, but memberships are most often renewed before they expire.

So if my expiration date is 2007-06-01 (date format, not unix time), and I wanted a add a year from THEN, try
[code]
update members set expDate =
concat( 
    date_format(expDate,'%Y')+1
  ,date_format(expDate,'-%m-%d')
        )
where mbrID=1;
[/code]

That gets you year beyond what I'm already due.
Be careful that if the membership is expired already not to shirtchange them, (use now)
[code]
concat( date_format(now(),'%Y')+1 ,date_format(now(),'-%m-%d'))
#year from now without php
[/code]

How you handle leap years is up to you. What you consider correct is correct.
This method grants the occasional 366 day-year.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.