php 在 PHP5 和 Actionscript3 中以毫秒为单位获取 unix 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1063513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting unix timestamp in milliseconds in PHP5 and Actionscript3
提问by Tom
In Actionscript, the Unix timestamp in milliseconds is obtainable like this:
在 Actionscript 中,以毫秒为单位的 Unix 时间戳可以这样获得:
public static function getTimeStamp():uint
{
var now:Date = new Date();
return now.getTime();
}
The doc clearly states the following:
该文档明确指出以下内容:
getTime():Number Returns the number of milliseconds since midnight January 1, 1970, universal time, for a Date object.
getTime():Number 为 Date 对象返回自 1970 年 1 月 1 日午夜(通用时间)以来的毫秒数。
When I trace it, it returns the following:
当我跟踪它时,它返回以下内容:
824655597
So, 824655597 / 1000 / 60 / 60 / 24 / 365 = 0.02 years. This is obviously not correct, as it should be around 39 years.
因此,824655597 / 1000 / 60 / 60 / 24 / 365 = 0.02 年。这显然是不正确的,因为它应该是 39 年左右。
Question #1: What's wrong here?
问题 1:这里有什么问题?
Now, onto the PHP part: I'm trying to get the timestamp in milliseconds there as well. The microtime()function returns either a string (0.29207800 1246365903) or a float (1246365134.01), depending on the given argument. Because I thought timestamps were easy, I was going to do this myself. But now that I have tried and noticed this float, and combine that with my problems in Actionscript I really have no clue.
现在,到 PHP 部分:我也在尝试以毫秒为单位获取时间戳。该microtime()函数返回字符串 (0.29207800 1246365903) 或浮点数 (1246365134.01),具体取决于给定的参数。因为我认为时间戳很容易,所以我打算自己做。但是现在我已经尝试并注意到了这个浮动,并将它与我在 Actionscript 中的问题结合起来,我真的不知道。
Question #2: how should I make it returns the amount of milliseconds in a Unix timestamp?
问题 #2:我应该如何让它返回 Unix 时间戳中的毫秒数?
Timestamps should be so easy, I'm probably missing something.. sorry about that. Thanks in advance.
时间戳应该很简单,我可能遗漏了一些东西……抱歉。提前致谢。
EDIT1:Answered the first question by myself. See below.
EDIT2:Answered second question by myself as well. See below. Can't accept answer within 48 hours.
EDIT1:我自己回答了第一个问题。见下文。
EDIT2:我自己也回答了第二个问题。见下文。48小时内不能接受答复。
采纳答案by jitter
For actionscript3, new Date().getTime()should work.
对于 actionscript3,new Date().getTime()应该可以工作。
In PHP you can simply call time()to get the time passed since January 1 1970 00:00:00 GMT in seconds. If you want milliseconds just do (time()*1000).
在 PHP 中,您可以简单地调用time()以获取自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的时间(以秒为单位)。如果你想要毫秒就做(time()*1000).
If you use microtime()multiply the second part with 1000 to get milliseconds. Multiply the first part with 1000 to get the milliseconds and round that. Then add the two numbers together. Voilá.
如果您使用microtime()将第二部分乘以 1000 以获得毫秒。将第一部分乘以 1000 得到毫秒数并四舍五入。然后将两个数字相加。瞧。
回答by Tom
I used unsigned integer as the return type of the function. This should be Number.
我使用无符号整数作为函数的返回类型。这应该是数字。
public static function getTimeStamp():Number
{
var now:Date = new Date();
return now.getTime();
}
Think I got the function for getting milliseconds in PHP5 now.
想我现在得到了在 PHP5 中获取毫秒的功能。
function msTimeStamp() {
return round(microtime(1) * 1000);
}
回答by Saurabh Kumar
Use this:
用这个:
intval(microtime(true)*1000)
回答by bob
To normalize a timestamp as an integer with milliseconds between Javascript, Actionscript, and PHP
在 Javascript、Actionscript 和 PHP 之间将时间戳标准化为整数,以毫秒为单位
Javascript / Actionscript:
Javascript / 动作脚本:
function getTimestamp(){
var d = new Date();
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()).valueOf();
}
PHP:
PHP:
function getTimestamp(){
$seconds = microtime(true); // true = float, false = weirdo "0.2342 123456" format
return round( ($seconds * 1000) );
}
See PHP note at "ben at sixg dot com's" comment at: http://www.php.net/manual/en/function.gmmktime.php
请参阅“ben at Sixg dot com”评论中的 PHP 注释:http://www.php.net/manual/en/function.gmmktime.php
EXCERPT: For most intents and purposes you can imagine that mktime() first converts your input parameters to GMT and then calls gmmktime() which produces a GMT timestamp.
摘录:对于大多数意图和目的,您可以想象 mktime() 首先将您的输入参数转换为 GMT,然后调用生成 GMT 时间戳的 gmmktime()。
So, time() always will return the same thing at the same actual moment, anywhere in the world.
gmmktime() and mktime(), when given specific time parameters, convert those time parameters FROM the appropriate timezone (GMT for gmmktime(), local time for mktime()), before computing the appropriate timestamp.
因此, time() 总是会在世界任何地方的同一实际时刻返回相同的内容。
gmmktime() 和 mktime(),当给定特定时间参数时,在计算适当的时间戳之前,从适当的时区(gmmktime() 为 GMT,mktime() 为本地时间)转换这些时间参数。
UPDATE:
更新:
On some versions of PHP, the timestamp with milliseconds is too large to display as a string. So use the sprintf function to get the string value:
在某些版本的 PHP 上,以毫秒为单位的时间戳太大而无法显示为字符串。所以使用 sprintf 函数来获取字符串值:
PHP
PHP
function getTimestamp($asString=false){
$seconds = microtime(true); // false = int, true = float
$stamp = round($seconds * 1000);
if($asString == true){
return sprintf('%.0f', $stamp);
} else {
return $stamp;
}
}
回答by Knut Haugen
microtime() in php5 returns unix timestamp with microseconds as per microtime()and if the get_as_float argument is not provided, it gives you a string formatted as "msec sec" so the first part is the millisecond part and the second is the second part. Just split it in two and you get the two parts of the timestamp
microtime中()中的php5返回Unix时间戳和微秒按microtime中() ,并且如果未设置get_as_float参数,它给你格式化为“毫秒秒”的字符串,从而所述第一部分是毫秒部分,第二个是第二部分. 只需将其一分为二,即可获得时间戳的两部分
回答by Dave Crooke
Simple answer for PHP:
PHP的简单答案:
function exact_time() {
$t = explode(' ',microtime());
return ($t[0] + $t[1]);
}
回答by Ali Najafi
PHP 7 This function has its return type declared.
PHP 7 这个函数声明了它的返回类型。
function timestamp_ms(): int {
$times = gettimeofday();
$seconds = strval($times["sec"]);
$milliseconds = strval(floor($times["usec"]/1000));
$missingleadingzeros = 3-strlen($milliseconds);
if($missingleadingzeros >0){
for($i = 0; $i < $missingleadingzeros; $i++){
$milliseconds = '0'.$milliseconds;
}
}
return intval($seconds.$milliseconds);
}
PHP 5
PHP 5
function timestamp_ms() {
$times = gettimeofday();
$seconds = strval($times["sec"]);
$milliseconds = strval(floor($times["usec"]/1000));
$missingleadingzeros = 3-strlen($milliseconds);
if($missingleadingzeros >0){
for($i = 0; $i < $missingleadingzeros; $i++){
$milliseconds = '0'.$milliseconds;
}
}
return intval($seconds.$milliseconds);
}
回答by rodarmy
I recently had this problem to get a timestamp in milliseconds. To just multiply the unix timestamp by 1000 did not resolve the problem because i had to compare two database entrys very precicely. Aparently the php datetime object can′t handle milliseconds/microseconds but its stored in the datetime string anyway. So here is my solution:
我最近遇到了这个问题,需要以毫秒为单位获取时间戳。仅将 unix 时间戳乘以 1000 并不能解决问题,因为我必须非常精确地比较两个数据库条目。显然,php datetime 对象不能处理毫秒/微秒,但它无论如何都存储在日期时间字符串中。所以这是我的解决方案:
$dateObject = new \DateTime('2015-05-05 12:45:15.444', new \DateTimeZone('Europe/London'));
$millis = $dateObject->format('v');
echo $dateObject->getTimestamp()*1000+$millis;
$dateObject = new \DateTime('2015-05-05 12:45:15.444', new \DateTimeZone('Europe/London'));
$millis = $dateObject->format('v');
echo $dateObject->getTimestamp()*1000+$millis;
This should also work with microseconds if you use format->('u')(and of course multiply the timestamp by 1000000) instead. I hope you find this useful.
如果您使用format->('u')(当然,将时间戳乘以 1000000),这也应该适用于微秒。希望这个对你有帮助。
回答by Lewison Chen
when you need the millisecond in str format, I think you should use:
当您需要 str 格式的毫秒时,我认为您应该使用:
public function get_millisecond() {
list($milliPart, $secondPart) = explode(' ', microtime());
$milliPart = substr($milliPart, 2, 3);
return $secondPart . $milliPart;
}
this will fix the bug int some get millisecond example where the milli part is like : 0.056. some example convert the milli part to float, your will get 56 instead of 056. I think some one want 056.
这将修复 bug int some get 毫秒示例,其中毫部分类似于:0.056。一些示例将毫部分转换为浮点数,您将得到 56 而不是 056。我想有人想要 056。
especially when you need the millisecond to order some data.
特别是当您需要毫秒来订购某些数据时。
hope will help. :)
希望会有所帮助。:)
回答by John Foley
To get millisecond timestamp from PHP DateTime object:
从 PHP DateTime 对象获取毫秒时间戳:
<?php
date_default_timezone_set('UTC');
$d = new \DateTime('some_data_string');
$mts = $d->getTimestamp().substr($d->format('u'),0,3); // millisecond timestamp

