PHP 等效于 JavaScript getTime()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19017267/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:04:35  来源:igfitidea点击:

PHP equivalent of JavaScript getTime()

javascriptphpdatetime

提问by Mark Kennedy

Suppose I'm running the following JavaScript getTime() function

假设我正在运行以下 JavaScript getTime() 函数

<script language="JavaScript">
var $x = Math.round((new Date()).getTime()/1000);
</script>

What would the equivalent code in PHP look like?

PHP 中的等效代码是什么样的?

采纳答案by Emilio Gort

Unix time in php

php中的Unix时间

$time_stamp = time();
echo $time_stamp;

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

返回自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来以秒数衡量的当前时间。

回答by Salvador Dali

Try something like this:

尝试这样的事情:

echo number_format(microtime(true)*1000,0,'.','');

or because you are dividing by 1000, most probably you need this:

或者因为你要除以 1000,很可能你需要这个:

echo number_format(microtime(true),0,'.','');

回答by Mr. B.

<?php
$x = time(); // seconds
$x = microtime(); // milliseconds
?>

Check out time()and microtime().

检查time()microtime()

回答by Darren

You'd use the php function time(). It creates the timestamp for you.

您将使用 php 函数time()。它为您创建时间戳。

<?php 
    $time = time();
?>