7 位小數計算的 UTC 時間 (UTC time with 7 decimals calculation)


問題描述

7 位小數計算的 UTC 時間 (UTC time with 7 decimals calculation)

我需要在 PHP 7.3 中計算 2 個 UTC 時間值與 7 位小數之間的差異

我可以簡單地執行以下操作:

val1 = 20200205120415.6513380; //first timestamp
val2 = 20200205120415.6535670; //second timestamp
$diff = $val2 ‑ $val1; //should be difference between the 2 timestamps

上述計算的值為 0.002229 . 如果我做得正確,該值以秒或微秒為單位,我能否將其轉換為 UNIX 紀元時間戳?


參考解法

方法 1:

I strongly suspect that the above times are not simple numbers; they BCD (binary coded decimal) for 2020‑02‑05‑12:04:15.6513380. You can't do simple math on these, and you'll need to parse them to convert to a unix timestamp.

Depending on your language, it may be easiest to parse these by turning them into strings and taking the first four characters as the year, the next two as the month, etc.

方法 2:

Here is my current solution for completeness.

The values on the right of the . is indeed fractional seconds. So in PHP to get the difference I did the following:

$start = 20200205120415.6513380;
$end = 20200205120415.6535670;

//get value left of . and then create datetime object to later convert to seconds
list($datetime, $usecStart) = explode(".", $start);
$startTime = date_create_from_format("YmdHis", $datetime);
list($datetime, $usecEnd) = explode(".", $end);
$endTime = date_create_from_format("YmdHis", $datetime);

//get timestamp in seconds and add franction or microseconds back
$start = $startTime‑>getTimestamp().".".$usecStart;
$end = $endTime‑>getTimestamp().".".$usecEnd;

//get difference in seconds and fraction or microseconds
echo $end ‑ $start;

Here is another way using datetime‑>diff() function:

$start = new DateTime('2020‑02‑05T12:04:15.6513380Z');
$end = new DateTime('2020‑02‑05T12:04:15.6535670Z');

$diff = $start‑>diff($end);
echo $diff‑>format('%h:%i:%s.%F');

(by Bobster101Rob NapierBobster101)

參考文件

  1. UTC time with 7 decimals calculation (CC BY‑SA 2.5/3.0/4.0)

#utc #unix #PHP #epoch #seconds






相關問題

Java - 從外部服務器獲取 POSIX UTC 時間戳的最佳選擇? (Java - Best choice to get a POSIX UTC timestamp from external server?)

PHP - Параўнанне лакальнага і UTC часу са зрушэннем гадзіннага пояса (PHP - Local vs UTC Time Comparison with Timezone Offset)

以 UTC 格式轉換日期時間 (Convert date time in utc)

如何確定日期字符串是否包含已在 Javascript 中添加的時間偏移量? (How to figure out if the Date string contains time offset added already in Javascript?)

Delphi - Tải TTimeZone cho múi giờ không thuộc địa phương và chuyển đổi giữa các múi giờ (Delphi - Get TTimeZone for non-local timezone and convert between timezones)

什麼是“標準”時區縮寫? (What are the "standard" timezone abbreviations?)

如何從包含 Oracle 中時區偏移的日期/時間字符串中獲取 UTC 日期/時間 (How to get UTC date/time from a date/time string that contains timezone offset in Oracle)

如何在不使用 Javascript 的情況下在 ASP.Net 中呈現給定 UTC 日期時間值的本地時間? (How to render local time given UTC datetime values in ASP.Net without using Javascript?)

如何讓 Java 解析和格式化具有相同時區的日期/時間?我一直在獲取本地時區 (How do I get Java to parse and format a date/time with the same time zone? I keep getting the local timezone)

Python - 從 DST 調整的本地時間到 UTC (Python - From DST-adjusted local time to UTC)

7 位小數計算的 UTC 時間 (UTC time with 7 decimals calculation)

我們應用程序中的日期格式 (Date format in our Application)







留言討論