• strtotime()使用注意事项


    strtotime在将一个潜在的时间字符串转换为正确的时间时,会使用到当前系统时区。如果系统时区与所给目标字符串所示时区不一样时,那么在调用后,其生成的时间将与想象中的时间有时间偏差。因此,需要在调用该方法前,首先确定当前系统时区。

    在需要设置时区的地方调用date_default_timezone_set()方法来设置时区。

    <?
        $str = 'Sat May 14 22:35:44 +0800 2011';
        $current_timezone = date_default_timezone_get();
        
        $strTtime = strtotime($str);
        
    ?>
    <table border = '1'>
            <caption>未设定时区前</caption>
            <tr>
                <td>时间字符串</td>
                <td><?=$str?></td>
            </tr>
            <tr>
                <td>当前时区</td>
                <td><?=$current_timezone?></td>
            </tr>
            <tr>
                <td>字符转化后日期格式('r')</td>
                <td><?=date('r',$strTtime)?></td>
            </tr>
            <tr>
                <td>字符转化后日期格式('H:i:s')</td>
                <td><?=date('H:i:s',$strTtime)?></td>
            </tr>
    </table>
    <br/></br>
    <?
      //设置时区 date_default_timezone_set('Asia/Shanghai'); $l_current_timezone = date_default_timezone_get(); $l_strTtime = strtotime($str); ?> <table border = '1'> <caption>设定时区后</caption> <tr> <td>时间字符串</td> <td><?=$str?></td> </tr> <tr> <td>当前时区</td> <td><?=$l_current_timezone?></td> </tr> <tr> <td>字符转化后日期格式('r')</td> <td><?=date('r',$l_strTtime)?></td> </tr> <tr> <td>字符转化后日期格式('H:i:s')</td> <td><?=date('H:i:s',$l_strTtime)?></td> </tr> </table> <? //使用完成后,还原到系统默认的设置 date_default_timezone_set($current_timezone); ?>
  • 相关阅读:
    windows 按时自动化任务
    Linux libusb 安装及简单使用
    Linux 交换eth0和eth1
    I.MX6 GPS JNI HAL register init hacking
    I.MX6 Android mmm convenient to use
    I.MX6 GPS Android HAL Framework 调试
    Android GPS GPSBasics project hacking
    Python windows serial
    【JAVA】别特注意,POI中getLastRowNum() 和getLastCellNum()的区别
    freemarker跳出循环
  • 原文地址:https://www.cnblogs.com/1500418882qqcom/p/13604805.html
Copyright © 2020-2023  润新知