PHP 7.1中DateTime::__construct()是否支持毫秒

查看:128
本文介绍了PHP 7.1中DateTime::__construct()是否支持毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

有信息显示在7.1版本中,DateTime::__construct()已经可以支持毫秒。并且专门强调:

This means that naive comparisons of two newly created instances will now more likely return FALSE instead of TRUE: new DateTime() == new DateTime()

可是,在我的代码中,毫秒部分总是000000,这个是什么问题呢:

<?php
for ($i = 0; $i < 10000; ++$i) {
    $date = new DateTime();
//    if ($date->format('u') != '000000') {
        file_put_contents('./msec.log', "$i:\t" . $date->format('s.u') . PHP_EOL, FILE_APPEND);
//    }
}

输出:

0:      51.000000
1:      51.000000
2:      51.000000
……
4999:   51.000000
5000:   51.000000
5001:   51.000000
……
9997:   51.000000
9998:   51.000000
9999:   51.000000

解决方案

我本地测试了。

  • 7.0 ,显示 ‘000000’

  • 7.1 ,显示真实数据了。
    所以,你运行代码的环境确定是 7.1 么。

这篇关于PHP 7.1中DateTime::__construct()是否支持毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆