简体   繁体   English

如何从PHP中的服务器获取日期和时间

[英]How to get date and time from server in php

my problem is when the time and date stored in database the result in database is 0000-00-00 00:00:00.000000. 我的问题是,当时间和日期存储在数据库中时,数据库中的结果是0000-00-00 00:00:00.000000。 how do the results match with the current time and date??? 结果如何与当前时间和日期匹配???

for this i used following the code: 为此,我使用了以下代码:

$dateandtime = $_SERVER['REQUEST_TIME']; $ dateandtime = $ _SERVER ['REQUEST_TIME'];

Use date() as the function formats a local date and time, and returns the formatted date string. 使用date()作为函数格式化本地日期和时间,并返回格式化的日期字符串。 Example:- 例:-

  $date = date('m/d/Y H:i:s ', time());

This prints date and time in 24 hour format. 这将以24小时格式打印日期和时间。 For 12 hour with am/pm use:- 上午/下午使用12小时:

 $date = date('m/d/Y h:i:s A', time());

I hope it help's you 希望对你有帮助

you try this code : 您尝试以下代码:

$now = DateTime::createFromFormat('U.u', microtime(true));
echo $now->format("m-d-Y H:i:s.u");

You can readily do this this with the input format Uu 您可以使用输入格式Uu轻松完成此操作

$now = DateTime::createFromFormat('U.u', microtime(true)); $ now = DateTime :: createFromFormat('U.u',microtime(true)); echo $now->format("mdY H:i:su"); echo $ now-> format(“ mdY H:i:su”);

This produces the following output: 这将产生以下输出:

04-13-2015 05:56:22.082300 From the PHP manual page for date formats: 2015年4月13日05:56:22.082300从PHP手册页中获取日期格式:

U = Seconds since the Unix Epoch, u = Microseconds U =自Unix纪元以来的秒数,u =微秒

Microseconds (added in PHP 5.2.2). 微秒(在PHP 5.2.2中添加)。 Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. 请注意,由于date()会采用整数参数,因此它将始终生成000000,而DateTime :: format()的确支持微秒(如果DateTime是用微秒创建的)。

您插入的格式错误,应为Ymd H:i:s ,不要被名称与PHP中的“ timestamp”相似而与MySQL中的“ timestamp”不同的事实所迷惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM