dongshuang0011 2014-06-23 15:33
浏览 51

与日期的PHP致命错误

This is my function:

public static function GetFormattedMicroTime($timestamp = false) {
    if(!$timestamp)
        $timestamp = microtime(true);
    return DateTime::createFromFormat('U.u',sprintf('%.f', $timestamp))->format('Y-m-d\TH:i:s.u');
}

This is how I call it (I'm calling it only from one point in my code):

$microtime = microtime(true);
$sinceLastLog = number_format($microtime - self::$lastLogTime,4);
self::$lastLogTime = $microtime;
return Utils::GetFormattedMicroTime($microtime) .'|'. $sinceLastLog .'| blah blah';

And I'm getting a lot of:

PHP message: PHP Fatal error: Call to a member function format() on a non-object

Can anybody help me troubleshoot?

EDIT:

I'm using php-fpm 5.4.28

i've changed my function a bit:

public static function GetFormattedMicroTime($timestamp = false) {
    if(!$timestamp)
        $timestamp = microtime(true);

    $d = DateTime::createFromFormat('U.u',sprintf('%.f', $timestamp));
    if ($d instanceof DateTime){
        return $d->format('Y-m-d\TH:i:s.u');
    } else {
        error_log("Erreur date ! timestamp = $timestamp erreur =".print_r(DateTime::getLastErrors(),true));
        return $timestamp;
    }
}

now i'm getting in nginx log:

PHP message: Erreur date ! timestamp = 1403539343,372 erreur = Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )
    [error_count] => 2
    [errors] => Array
        (
            [10] => Unexpected data found.
        )
)
  • 写回答

1条回答 默认 最新

  • doudi4621 2014-06-23 18:04
    关注

    The locale affects the output of sprintf. Apparently, wherever you're getting this error from has a locale that uses a comma instead of a decimal point to separate whole and fractional digits. Essentially, it's trying to do this:

    DateTime::createFromFormat('U.u','1403538707,536290')
    

    createFromFormat just sees it as a decimal point character, not as the locale's decimal separator.

    Try using number_format instead.

    DateTime::createFromFormat('U.u', number_format($timestamp, 6, '.', ''));
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP