在 Actionscript 中,以毫秒为单位的 Unix 时间戳可以这样获取:
public static function getTimeStamp():uint
{
var now:Date = new Date();
return now.getTime();
}
该文档明确指出以下内容:
getTime():Number 返回时间的数量 自 1 月 1 日午夜以来的毫秒数, 1970 年,世界时间,约会 对象。
当我跟踪它时,它返回以下内容:
824655597
所以,824655597 / 1000 / 60 / 60 / 24 / 365 = 0.02 年。 这显然是不正确的,应该是39岁左右。
问题#1:这里出了什么问题?
现在,进入 PHP 部分:我也试图在那里获取以毫秒为单位的时间戳。
microtime()
函数返回字符串 (0.29207800 1246365903) 或浮点数 (1246365134.01),具体取决于给定的参数。因为我认为时间戳很简单,所以我打算自己做这件事。但现在我已经尝试并注意到了这个浮动,并将其与我在 Actionscript 中的问题结合起来,我真的不知道了。
问题#2:我应该如何让它返回 Unix 时间戳中的毫秒数?
时间戳应该很简单,我可能错过了一些东西..对此感到抱歉。预先感谢。
编辑1:我自己回答了第一个问题。见下文。
EDIT2:我自己也回答了第二个问题。见下文。 48小时内无法接受答复。
我使用无符号整数作为函数的返回类型。这应该是数字。
public static function getTimeStamp():Number
{
var now:Date = new Date();
return now.getTime();
}
我现在已经有了 PHP5 中获取毫秒的函数了。
function msTimeStamp() {
return round(microtime(1) * 1000);
}
对于actionscript3,
new Date().getTime()
应该可以工作。
在 PHP 中,您只需调用 time() 即可获取自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的时间(以秒为单位)。如果你想要毫秒,就做
(time()*1000)
。
如果使用 microtime() 将第二部分乘以 1000 即可得到毫秒。将第一部分乘以 1000 即可得到毫秒并将其四舍五入。然后将两个数字相加。瞧。
使用这个:
intval(microtime(true)*1000)
将 Javascript、Actionscript 和 PHP 之间的时间戳标准化为以毫秒为单位的整数
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:
function getTimestamp(){
$seconds = microtime(true); // true = float, false = weirdo "0.2342 123456" format
return round( ($seconds * 1000) );
}
请参阅“ben at Sixg dot com's”评论中的 PHP 注释:http://www.php.net/manual/en/function.gmmktime.php
摘录: 对于大多数意图和目的,您可以想象 mktime() 首先将您的输入参数转换为 GMT,然后调用 gmmktime() 生成 GMT 时间戳。
因此,time() 总是会在世界任何地方的同一实际时刻返回相同的内容。
gmmktime() 和 mktime(),当给定特定时间参数时,在计算适当的时间戳之前,将这些时间参数从适当的时区(gmmktime() 为 GMT,mktime() 为本地时间)转换。
在某些版本的 PHP 上,以毫秒为单位的时间戳太大,无法显示为字符串。所以使用sprintf函数来获取字符串值:
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;
}
}
microtime() 按照 microtime() 返回带有微秒的 unix 时间戳,如果未提供 get_as_float 参数,它会为您提供一个格式为“msec sec”的字符串,因此第一部分是毫秒部分,第二部分是第二部分。只需将其分成两部分即可获得时间戳的两部分
PHP 的简单答案:
function exact_time() {
$t = explode(' ',microtime());
return ($t[0] + $t[1]);
}
从 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
这是函数的改进版本,确保与 PHP 7 的兼容性,同时保留 PHP 5 的功能:
function timestamp_ms(): int {
$times = gettimeofday();
$seconds = sprintf('%d', $times["sec"]); // Use sprintf for integer formatting
$milliseconds = sprintf('%03d', floor($times["usec"]/1000)); // Use sprintf for zero-padding
return intval($seconds . $milliseconds);
}
当您需要 str 格式的毫秒时,我认为您应该使用:
public function get_millisecond() {
list($milliPart, $secondPart) = explode(' ', microtime());
$milliPart = substr($milliPart, 2, 3);
return $secondPart . $milliPart;
}
这将修复一些获取毫秒示例中的错误,其中毫秒部分如下:0.056。 一些示例将毫部分转换为浮点数,您将得到 56 而不是 056。我认为有人想要 056。
尤其是当您需要毫秒来订购一些数据时。
希望能有所帮助。 :)
我最近遇到了获取毫秒时间戳的问题。仅将 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;
如果您使用
format->('u')
(当然还要将时间戳乘以 1000000),这也应该适用于微秒。我希望您觉得这很有用。
类似这样的:
$mili_sec_time = $_SERVER['REQUEST_TIME_FLOAT'] * 1000;
提供浮点类型,表示从 UNIX 纪元到请求开始的毫秒数。
$timestamp = str_replace(".","",number_format((float)microtime(true),2,'.',''));