如何在php中减去毫秒和显示日期(以毫秒为单位)?

编程入门 行业动态 更新时间:2024-10-25 22:27:44
本文介绍了如何在php中减去毫秒和显示日期(以毫秒为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在php中减去毫秒和显示日期的毫秒数?

例如:我设置了结束日期和时间

$endtime = 2012-02-21 10:29:59;

那么我有当前日期或开始日期,是从微时间转换的

$starttime = 2012-02-21 10:27:59.452; function getTimestamp() { $microtime = floatval(substr((string)microtime(), 1, 8)); $rounded = round($microtime, 3); return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded)); } echo getTimestamp(); //sample output 2012-02-21 10:27:59.452

现在我要减去: $ finaldate = $ endtime-$ starttime;

我希望输出如下: 00:00:02.452

解决方案

您需要使用microtime作为开始/结束值,并且仅设置其格式以在末尾显示.

// Get the start time in microseconds, as a float value $starttime = microtime(true); /************/ /* Do stuff */ /************/ // Get the difference between start and end in microseconds, as a float value $diff = microtime(true) - $starttime; // Break the difference into seconds and microseconds $sec = intval($diff); $micro = $diff - $sec; // Format the result as you want it // $final will contain something like "00:00:02.452" $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.3f', $micro));

注意:这是从microtime返回浮点值,并使用浮点算术来简化数学,因此由于浮点舍入问题,您的数字可能会略有偏离,但是您将结果舍入为3位数字无论如何,最后,处理器时序的微小波动还是比浮点错误大,因此这对您来说在多个级别上都不是问题.

How to subtract microtime and display date with milliseconds in php ?

For example: I have set end date and time

$endtime = 2012-02-21 10:29:59;

then I have current date or start date with converted from microtime

$starttime = 2012-02-21 10:27:59.452; function getTimestamp() { $microtime = floatval(substr((string)microtime(), 1, 8)); $rounded = round($microtime, 3); return date("Y-m-d H:i:s") . substr((string)$rounded, 1, strlen($rounded)); } echo getTimestamp(); //sample output 2012-02-21 10:27:59.452

Now I want to subtract: $finaldate = $endtime - $starttime;

I want my output to be like this: 00:00:02.452

解决方案

You need to use microtime for the start/end values, and only format it for display at the end.

// Get the start time in microseconds, as a float value $starttime = microtime(true); /************/ /* Do stuff */ /************/ // Get the difference between start and end in microseconds, as a float value $diff = microtime(true) - $starttime; // Break the difference into seconds and microseconds $sec = intval($diff); $micro = $diff - $sec; // Format the result as you want it // $final will contain something like "00:00:02.452" $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.3f', $micro));

Note: this is returning float values from microtime and using float arithmetic to simplify the math, so your numbers may be extremely slightly off due to the float rounding problem, but you are rounding the result to 3 digits in the end anyway, and minor fluctuations in processor timing are greater than floating point errors anyway, so this is not problem for you on multiple levels.

更多推荐

如何在php中减去毫秒和显示日期(以毫秒为单位)?

本文发布于:2023-10-30 22:26:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   单位   如何在   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!