Forum Moderators: coopster

Message Too Old, No Replies

Date plus 1 year

Adding 1 year to date

         

webwonderuk

12:19 am on Jan 6, 2004 (gmt 0)

10+ Year Member



I am writing the date to my database and would like a second field exactly 1 year from the date that row was created, i am using timestamp(6) for the date which is fine, but for the future date i dont really want timestamp because the rows get updated and consequently the dates..
It is basically a membership scheme with one years use of an item so the second date will be the end of the subscription.
Or possibly some other way round the problem...

jatar_k

2:43 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, you could go about it a few ways

you could not use the timestamp and use a date field and on access do the math to see if it is expired.

You could also store 2 dates. I don't really see why you want the purchase/registration date to be a timestamp but I assume you have a reason for that.

I would imagine you are using something like NOW() in your insert statement at present. You could do something like so

$yrplus1 = strtotime [ca.php.net]("+1 year");

that will put a unix timestamp into the variable $yrplus1 that is equal to 1 yr from that second. You can then pop that into a column as is or format it however you like. It is easy to just store the unix timestamp so it is easy to compare.

dcrombie

11:39 am on Jan 6, 2004 (gmt 0)



PostgreSQL:

INSERT INTO table (expires) VALUES ('now'::date + INTERVAL '1 year');
INSERT INTO table (expires) VALUES (NOW() + INTERVAL '1 year');

MySQL:

INSERT INTO table (expires) VALUES (NOW() + INTERVAL 1 year);

(note that 'quotes' are required for psql and not allowed for mysql)

g1smd

11:02 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How does +1year handle non-leap years for entries made on Feb 29th in a leap year (like in 2004)?

coopster

12:49 am on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If it is a subscription or anniversary date, why even store the second date? Just check the date during script execution to see if anniversary date has been reached. I guess I am questioning whether or not you even need to store that data?

g1smd

9:29 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> I guess I am questioning whether or not you even need to store that data? <<

So was I, in a roundabout sort of way. Unless you are very careful with that data and the interaction with leap years there are some circumstances where some subscribers may receive 11 or 13 issues instead of the expected 12.