PHP DateTime 마이크로 초는 항상 0을 반환합니다.
이 코드는 PHP 5.2.5에서 마이크로 초 동안 항상 0을 반환합니다.
<?php
$dt = new DateTime();
echo $dt->format("Y-m-d\TH:i:s.u") . "\n";
?>
산출:
[root@www1 ~]$ php date_test.php
2008-10-03T20:31:26.000000
[root@www1 ~]$ php date_test.php
2008-10-03T20:31:27.000000
[root@www1 ~]$ php date_test.php
2008-10-03T20:31:27.000000
[root@www1 ~]$ php date_test.php
2008-10-03T20:31:28.000000
어떤 아이디어?
http://us.php.net/date 가 마이크로 초 지정자를 문서화하고 있지만 실제로 지원하지 않는 것은 비논리적으로 보이지만 작동하는 것 같습니다 .
function getTimestamp()
{
return date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
}
DateTime
객체를 생성 할 때 입력에 마이크로 초가 포함되도록 지정하고 입력으로 microtime(true)
직접 사용할 수 있습니다.
안타깝게도 정확한 1 초를 맞추면 실패 할 것입니다. 왜냐하면 .
마이크로 타임 출력 에는 없기 때문입니다 . sprintf
이 경우에 포함하도록 강제로 사용하십시오 .0
.
date_create_from_format(
'U.u', sprintf('%.f', microtime(true))
)->format('Y-m-d\TH:i:s.uO');
또는 동등하게 (더 많은 OO 스타일)
DateTime::createFromFormat(
'U.u', sprintf('%.f', microtime(true))
)->format('Y-m-d\TH:i:s.uO');
이 함수는 http://us3.php.net/date 에서 가져 왔습니다.
function udate($format, $utimestamp = null)
{
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
echo udate('H:i:s.u'); // 19:40:56.78128
"u"가 작동하도록하려면이 함수를 구현해야합니다 ... : \
이것을 시도하면 마이크로 초가 표시됩니다.
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
print $d->format("Y-m-d H:i:s.u");
\DateTime::createFromFormat('U.u', microtime(true));
(적어도 대부분의 시스템에서) 다음을 제공합니다.
object(DateTime)(
'date' => '2015-03-09 17:27:39.456200',
'timezone_type' => 3,
'timezone' => 'Australia/Darwin'
)
그러나 PHP 부동 소수점 반올림으로 인해 정밀도가 손실됩니다. 진정한 마이크로 초가 아닙니다.
최신 정보
이것은 아마도 createFromFormat()
옵션 의 가장 좋은 절충안이며 완전한 정밀도를 제공합니다.
\DateTime::createFromFormat('0.u00 U', microtime());
gettimeofday ()
더 명확하고 더 강력 할 수 있습니다. Xavi가 발견 한 버그를 해결합니다.
$time = gettimeofday();
\DateTime::createFromFormat('U.u', sprintf('%d.%06d', $time['sec'], $time['usec']));
맞아, 나는 이것을 단번에 정리하고 싶다.
PHP에서 ISO 8601 형식 날짜 및 시간을 밀리 초 및 마이크로 초로 표시하는 방법에 대한 설명 ...
밀리 초 또는 'ms'는 소수점 뒤에 4 자리 숫자 (예 : 0.1234)가 있습니다. 마이크로 초 또는 'µs'는 소수점 뒤에 7 자리 숫자가 있습니다. 여기에 초 분수 / 이름 설명
PHP의 date()
함수는 'u'형식 문자 아래 의 PHP 날짜 문서 에 설명 된대로 정수만 제외하고 밀리 초 또는 마이크로 초로 예상대로 완전히 작동하지 않습니다 .
Lucky의 주석 아이디어 ( 여기 )를 기반으로 하지만 수정 된 PHP 구문과 적절한 초 형식 처리 (Lucky의 코드는 초 뒤에 잘못된 추가 '0'을 추가했습니다)
또한 경쟁 조건을 제거하고 초를 올바르게 형식화합니다.
밀리 초의 PHP 날짜
동등한 작업 date('Y-m-d H:i:s').".$milliseconds";
list($sec, $usec) = explode('.', microtime(true));
echo date('Y-m-d H:i:s.', $sec) . $usec;
출력 = 2016-07-12 16:27:08.5675
마이크로 초가있는 PHP 날짜
날짜 함수가 마이크로 초 / / 'u' 와 동일하게 작동 date('Y-m-d H:i:s').".$microseconds";
하거나 date('Y-m-d H:i:s.u')
예상대로 작동하는지 여부microtime()
list($usec, $sec) = explode(' ', microtime());
echo date('Y-m-d H:i:s', $sec) . substr($usec, 1);
출력 = 2016-07-12 16:27:08.56752900
이것은 나를 위해 일했으며 간단한 3 줄입니다.
function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
if(NULL === $microtime) $microtime = microtime();
list($microseconds,$unix_time) = explode(' ', $microtime);
return date($format,$unix_time) . array_pop(explode('.',$microseconds));
}
이것은 기본적으로 (제공된 매개 변수 없음) 현재 호출 된 마이크로 초 동안 다음 형식의 문자열을 반환합니다.
YYYY-MM-DD HH : MM : SS.UUUUUUUU
더 간단하고 빠른 것 (정확도가 절반에 불과하지만)은 다음과 같습니다.
function udate($format='Y-m-d H:i:s.', $microtime=NULL) {
if(NULL === $microtime) $microtime = microtime(true);
list($unix_time,$microseconds) = explode('.', $microtime);
return date($format,$unix_time) . $microseconds;
}
이것은 다음 형식으로 인쇄됩니다.
YYYY-MM-DD HH : MM : SS.UUUU
time : strtotime ()에서 허용하는 형식의 문자열, 기본값은 "now"입니다.
time : GNU»날짜 입력 형식 구문에 따라 구문 분석 할 문자열입니다. PHP 5.0.0 이전에는 마이크로 초가 허용되지 않았습니다. PHP 5.0.0부터는 허용되지만 무시되었습니다.
에서 근무 행운 의 의견 이 는 PHP 버그 데이터베이스에 기능 요청 ,이 같은 것을 사용합니다
class ExtendedDateTime extends DateTime {
/**
* Returns new DateTime object. Adds microtime for "now" dates
* @param string $sTime
* @param DateTimeZone $oTimeZone
*/
public function __construct($sTime = 'now', DateTimeZone $oTimeZone = NULL) {
// check that constructor is called as current date/time
if (strtotime($sTime) == time()) {
$aMicrotime = explode(' ', microtime());
$sTime = date('Y-m-d H:i:s.' . $aMicrotime[0] * 1000000, $aMicrotime[1]);
}
// DateTime throws an Exception with a null TimeZone
if ($oTimeZone instanceof DateTimeZone) {
parent::__construct($sTime, $oTimeZone);
} else {
parent::__construct($sTime);
}
}
}
$oDate = new ExtendedDateTime();
echo $oDate->format('Y-m-d G:i:s.u');
산출:
2010-12-01 18:12:10.146625
이건 어때?
$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";
샘플 출력
2013-07-17 08:23:37:0.88862400
This should be the most flexible and precise:
function udate($format, $timestamp=null) {
if (!isset($timestamp)) $timestamp = microtime();
// microtime(true)
if (count($t = explode(" ", $timestamp)) == 1) {
list($timestamp, $usec) = explode(".", $timestamp);
$usec = "." . $usec;
}
// microtime (much more precise)
else {
$usec = $t[0];
$timestamp = $t[1];
}
// 7 decimal places for "u" is maximum
$date = new DateTime(date('Y-m-d H:i:s' . substr(sprintf('%.7f', $usec), 1), $timestamp));
return $date->format($format);
}
echo udate("Y-m-d\TH:i:s.u") . "\n";
echo udate("Y-m-d\TH:i:s.u", microtime(true)) . "\n";
echo udate("Y-m-d\TH:i:s.u", microtime()) . "\n";
/* returns:
2015-02-14T14:10:30.472647
2015-02-14T14:10:30.472700
2015-02-14T14:10:30.472749
*/
String in a format accepted by strtotime() It work!
Inside of an application I am writing I have the need to set/display microtime on DateTime objects. It seems the only way to get the DateTime object to recognize microseconds is to initialize it with the time in format of "YYYY-MM-DD HH:MM:SS.uuuuuu". The space in between the date and time portions can also be a "T" as is usual in ISO8601 format.
The following function returns a DateTime object initialized to the local timezone (code can be modified as needed of course to suit individual needs):
// Return DateTime object including microtime for "now"
function dto_now()
{
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec, 2, 6);
$datetime_now = date('Y-m-d H:i:s\.', $sec).$usec;
return new DateTime($datetime_now, new DateTimeZone(date_default_timezone_get()));
}
PHP documentation clearly says "Note that date() will always generate 000000 since it takes an integer parameter...". If you want a quick replacement for date()
function use below function:
function date_with_micro($format, $timestamp = null) {
if (is_null($timestamp) || $timestamp === false) {
$timestamp = microtime(true);
}
$timestamp_int = (int) floor($timestamp);
$microseconds = (int) round(($timestamp - floor($timestamp)) * 1000000.0, 0);
$format_with_micro = str_replace("u", $microseconds, $format);
return date($format_with_micro, $timestamp_int);
}
(available as gist here: date_with_micro.php)
Building on Lucky’s comment, I wrote a simple way to store messages on the server. In the past I’ve used hashes and increments to get unique file names, but the date with micro-seconds works well for this application.
// Create a unique message ID using the time and microseconds
list($usec, $sec) = explode(" ", microtime());
$messageID = date("Y-m-d H:i:s ", $sec) . substr($usec, 2, 8);
$fname = "./Messages/$messageID";
$fp = fopen($fname, 'w');
This is the name of the output file:
2015-05-07 12:03:17 65468400
Some answers make use of several timestamps, which is conceptually wrong, and overlapping issues may occur: seconds from 21:15:05.999
combined by microseconds from 21:15:06.000
give 21:15:05.000
.
Apparently the simplest is to use DateTime::createFromFormat()
with U.u
, but as stated in a comment, it fails if there are no microseconds.
So, I'm suggesting this code:
function udate($format, $time = null) {
if (!$time) {
$time = microtime(true);
}
// Avoid missing dot on full seconds: (string)42 and (string)42.000000 give '42'
$time = number_format($time, 6, '.', '');
return DateTime::createFromFormat('U.u', $time)->format($format);
}
This method is safer than the accepted answer:
date('Y-m-d H:i:s.') . str_pad(substr((float)microtime(), 2), 6, '0', STR_PAD_LEFT)
Output:
2012-06-01 12:00:13.036613
Update: Not recommended (see comments)
date('u') is supported only from PHP 5.2. Your PHP may be older!
참고URL : https://stackoverflow.com/questions/169428/php-datetime-microseconds-always-returns-0
'program tip' 카테고리의 다른 글
std :: array에 배열을 채울 값을 취하는 생성자가없는 이유는 무엇입니까? (0) | 2020.11.02 |
---|---|
Python에서 QR 코드 이미지를 디코딩하는 방법은 무엇입니까? (0) | 2020.11.02 |
인터페이스의 명시 적 구현이 공개 될 수없는 이유는 무엇입니까? (0) | 2020.11.02 |
ASP.NET MVC : 숨겨진 필드 값은 HtmlHelper.Hidden을 사용하여 렌더링되지 않습니다. (0) | 2020.11.02 |
Java는 클래스와 동일합니다. (0) | 2020.11.02 |