php 在php中从计算机系统获取时间

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

get time from computer system in php

phptime

提问by Ravneet 'Abid'

I am trying to get time from computer system in php and I want it exactly as in computer , not other format.

我试图从 php 中的计算机系统中获取时间,我希望它与计算机中的完全一样,而不是其他格式。

I have already did it in javascript but it client side and I want server side. So I am trying it in php.

我已经在 javascript 中做到了,但它是客户端,我想要服务器端。所以我在 php 中尝试它。

Is it possible in php?

在php中可以吗?

回答by Michele Spagnuolo

If you mean the date/time of the server (the machine that runs PHP), you can use the datefunction.

如果您指的是服务器(运行 PHP 的机器)的日期/时间,则可以使用该date函数。

Directly from PHP documentation:

直接来自PHP 文档

<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>

If you mean from the client's computer, then it couldn't be done. PHP is server-side.

如果您的意思是从客户端的计算机,则无法完成。PHP 是服务器端的。

回答by Quentin

from client's computer.

从客户端的计算机。

Browsers do not send the current time with every request, so PHP running on the server has no way of knowing what the client clock is set to (unless you use JavaScript (or some other technology you can use client side) to collect the time and make an HTTP request to the server that includes it explicitly).

浏览器不会随每个请求发送当前时间,因此在服务器上运行的 PHP 无法知道客户端时钟设置为什么(除非您使用 JavaScript(或您可以使用客户端的其他技术)来收集时间和向显式包含它的服务器发出 HTTP 请求)。

回答by Matt Clark

PHP is a language that is executed on the server side of things, code that is never seen by the CLIENT's computer. The problem that you are trying to solve can not be done with PHP. To get the date/time of the server running the PHP, see the following: http://php.net/manual/en/function.getdate.php

PHP 是一种在服务器端执行的语言,是客户端计算机永远不会看到的代码。您试图解决的问题无法用 PHP 完成。要获取运行 PHP 的服务器的日期/时间,请参阅以下内容:http: //php.net/manual/en/function.getdate.php