Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 882
FinalForm Avatar asked Jul 19 '11 20:07

FinalForm


People also ask

How much is a month in timestamp?

One month = 2,419,200 for 28-day months, 2,505,600 for 29-day months, 2,592,000 for 30-day months and 2,678,400 for 31-day months. One year = 31,536,000 in UNIX time.

How do you calculate a timestamp from a date?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

What is Unix timestamp for a date?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

What is timestamp in Unix format?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.


2 Answers

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

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

like image 169
jondavidjohn Avatar answered Oct 27 '22 01:10

jondavidjohn


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 30*24*3600 seconds...


You could also work with the DateTime class, and methods such as DateTime::modify() or DateTime::add().

like image 43
Pascal MARTIN Avatar answered Oct 27 '22 00:10

Pascal MARTIN