user3745594
user3745594

Reputation: 43

How to update unix-timestamp in mysql database

I am trying to update the filed date in mysql database , this field contains unix-timestamp date.

But I do not want to convert the whole of date 1364562954 , I want to replace only the year and leave the day, month and time same.

For Example - 19-03-2015, 03:43 PM to 19-03-2016, 03:43 PM

Upvotes: 1

Views: 3404

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

You can use FROM_UNIXTIME and UNIX_TIMESTAMP to convert a Unix timestamp to MySQL's DATETIME format.

So for example to get a timestamp incremented by one year:

SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(1364562954) + INTERVAL 1 YEAR);

Result: 1396098954

For UPDATE it is the same:

UPDATE t SET tstamp= UNIX_TIMESTAMP(FROM_UNIXTIME(tstamp) + INTERVAL 1 YEAR);

Reference: Date and Time Functions

Upvotes: 2

Related Questions