douniang3866 2011-04-06 00:01
浏览 55
已采纳

从PHP中删除一小时(FROM_UNIXTIME)

Could you please advice how to remove an hour from unixtime.

I have a unixtime and i need to remove an extra hour before converting to normal time. Could you please advice how to do this?

Also, is Unixtime affected by the GMT time changes?

  • 写回答

1条回答 默认 最新

  • dongliang1654 2011-04-06 00:06
    关注

    Unix timestamps are measured in seconds, there are 3600 seconds in an hour (60 minutes, each with 60 seconds = 60*60 = 3600), so just subtract:

    $timestamp = time();
    $timestamp_less_one_hour = $timestamp - 3600;
    

    You can also use strtotime to accomplish the same:

    $timestamp_less_one_hour = strtotime('-1 hour', $timestamp);
    

    Or if $timestamp is simply "now" you can call strtotime without the second parameter:

    $timestamp_less_one_hour = strtotime('-1 hour'); // time() - 1 hour
    

    So you seem to be looking for GMT time instead, the trouble is GMT can include Daylight Savings Time (DST), and the "GMT" date functions in PHP actually return UTC time, which has no concept of DST. Luckily you can detect if it's DST using PHP, and basically adjust appropriately.

    Get UTC time using gmdate:

    $utc_time = gmdate('U'); // return Unix seconds in UTC timezone
    

    Detect if it's DST using I:

    $is_dst = gmdate('I'); // 1 when it's DST, 0 otherwise
    

    gmdate('U') will trail GMT time during DST by an hour, thus you need to give it +3600 seconds, or 1 hour when it's DST, so putting this together:

    $gmt_time = gmdate('U') + (3600*gmdate('I'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作