简体   繁体   中英

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. how do the results match with the current time and date???

for this i used following the code:

$dateandtime = $_SERVER['REQUEST_TIME'];

Use date() as the function formats a local date and time, and returns the formatted date string. Example:-

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

This prints date and time in 24 hour format. For 12 hour with am/pm use:-

 $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

$now = DateTime::createFromFormat('U.u', microtime(true)); 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:

U = Seconds since the Unix Epoch, u = Microseconds

Microseconds (added in 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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