简体   繁体   中英

Add and substract time from a timestamp

I send a timestamp to my my database, and when I get it back it shows me the time 7 hours earlier, because of the local time at the server's location.

I don't want to modify the server time since the timestamp has to show minutes or hours ago to different users in different countries.

The solution would be to add or subtract time depending the country. Is there a way to show the time of the post to different users in different timezones correctly with a simple function?

Or can you give me something to modify the timestamp?

As the UNIX timestamp that comes from PHP is defined as "time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)" you can just add or subtract any seconds from that and it'll be OK.

eg add one hour to timestamp:

$time = time() + 60*60;

equals

$time = time() + 3600;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM