Domanda

I've a field in MySQL table named last_date which has a data-type bigint(12). In this field I'm storing the equivalent UNIX Timestamp values of dates.

Now I want to insert UNIX Timestamp value equivalent to the five(5) months from today's date. That is if today's date is

13/02/2014(dd/mm/yyyy format, 13th February 2014)

I want to insert the UNIX Timestamp equivalent of the date

13/07/2014(dd/mm/yyyy format, 13th July 2014)

Also if the year change is required then also it should work. Means if today's date is

13/11/2014(dd/mm/yyyy format, 13th November 2014)

then the equivalent UNIX Timestamp to be inserted should be of date

13/04/2015(dd/mm/yyyy format, 13th April 2015)

Which means year change should also be considered. Do anyone have any idea of how to achieve this?

È stato utile?

Soluzione

MySql supports the use of INTERVAL for this type of operation

INSERT INTO table SET newdate = unix_timestamp(now() + INTERVAL 5 month);

Here is an example of it in action

mysql> select unix_timestamp(),unix_timestamp(now() + interval 5 month);
+------------------+------------------------------------------+
| unix_timestamp() | unix_timestamp(now() + interval 5 month) |
+------------------+------------------------------------------+
|       1392274422 |                               1405230822 |
+------------------+------------------------------------------+
1 row in set (0.00 sec)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top