douqiong8412 2015-03-03 10:33
浏览 366
已采纳

从时间戳添加和减去时间

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?

  • 写回答

1条回答 默认 最新

  • dtgr3392 2015-03-03 10:40
    关注

    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.

    e.g. add one hour to timestamp:

    $time = time() + 60*60;
    

    equals

    $time = time() + 3600;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部