Steve Nguyen
Steve Nguyen

Reputation: 5974

How to get the Unix Timestamp for one month in the future given a date

Suppose I pull a field from the database, a timestamp field.

How do I get the Unix Timestamp, 1 month in the future, from that timestamp field?

My brain is a bit tired, so I need a bit of a re-jogging.

Upvotes: 10

Views: 34584

Answers (5)

cwallenpoole
cwallenpoole

Reputation: 82028

Depends on the database.

(Don't know the others, sorry)

Of course there is also strtotime('+1 month', $current_time); in PHP. Of course, strtotime is so incredibly easy you might consider it cheating ;-).

Upvotes: 2

Ryland
Ryland

Reputation: 117

How about something like:

tm *currTime = localtime(timestamp);

currTime->tm_mon++;

timestamp = mktime(currTime);

Upvotes: -2

iLLin
iLLin

Reputation: 760

Wouldn't

strtotime('+1 month', $value_from_db); 

work?

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 401002

I would use the strtotime() function, to add +1 month to your timestamp :

$future = strtotime('+1 month', $current_time);

With `strtotime()`, you don't have to think yourself about stuff like "some month avec 30 days, and others have 31", or "february sometimes has 28 days and sometimes 29".

After all, adding one month to a date is a bit harder that adding 30243600 seconds...


You could also work with the [**`DateTime`**][2] class, and methods such as [`DateTime::modify()`][3] or [`DateTime::add()`][4].

Upvotes: 7

jondavidjohn
jondavidjohn

Reputation: 62392

$newDate = strtotime('+1 month',$startDate); 

Using strtotime() you can pass it '+1 month' to add a context sensitive month.

Upvotes: 25

Related Questions