如何在 PHP 中获取当前的日期和时间?

Which PHP function can return the current date/time?

转载于:https://stackoverflow.com/questions/470617/how-to-get-the-current-date-and-time-in-php

weixin_41568208
北城已荒凉 -1 because "this question does not show any research effort" (source - the tooltip when you hover over the downvote button). SO shouldn't be your immediate go-to whenever you have a question, you should at least RTFM first, or turn to Google (other search engines are available ;)).
一年多之前 回复
csdnceshi62
csdnceshi62 TLDR; $date = date('m/d/Y h:i:s a', time());
接近 2 年之前 回复

30个回答

The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions are called to.

I'm in Melbourne, Australia so I have something like this:

date_default_timezone_set('Australia/Melbourne');

Or another example is LA - US:

date_default_timezone_set('America/Los_Angeles');

You can also see what timezone the server is currently in via:

date_default_timezone_get();

So something like:

$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;

So the short answer for your question would be:

// Change the line below to your timezone!
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());

Then all the times would be to the timezone you just set :)

csdnceshi69
YaoRaoLov after all you want to save date/time in database, if that is what needed; why not just use the MySQL commands to deal with date/time. MySQL provides considerable flexibility in how dates and times are formatted. take this example: INSERT INTO orders (order_item, order_date, order_delivery) VALUES ('iPhone 8Gb', NOW(), DATE_ADD(NOW(), INTERVAL 14 DAY));. See Dates and Times in MySQL
2 年多之前 回复
csdnceshi68
local-host A simpler and more correct answer would not be to omit potentially important and related information, but instead to answer the question, as you suggest, but then provide additional information on timezones beneath it. There's no need to omit potentially crucial information just because it wasn't asked for.
接近 5 年之前 回复
csdnceshi60
℡Wang Yan You can get all the time zone from here Time Zone
大约 5 年之前 回复
csdnceshi70
笑故挽风 I actually appreciated the additional timezone information. It saved me a second search.
5 年多之前 回复
csdnceshi58
Didn"t forge OP never asked about timezone. A simpler and more correct answer would simply show server time.
接近 6 年之前 回复
csdnceshi55
~Onlooker America/Los_Angeles
6 年多之前 回复
csdnceshi74
7*4 Which one is PST?
6 年多之前 回复
weixin_41568184
叼花硬汉 Thanks! I was wondering how to change the time zone on the date() function :)
8 年多之前 回复
csdnceshi52
妄徒之命 The call to time() is redundant, date() will automatically use the current time.
大约 11 年之前 回复
$date = date('m/d/Y h:i:s a', time());

works, but how also to know if it's EST, PST?

weixin_41568208
北城已荒凉 I think the user's time would need to be obtained using Javascript since PHP runs on your web server and Javascript runs on their computer.
6 年多之前 回复
csdnceshi56
lrony* Actually now that I think about it the time isn't as important as the date. THANKS TO ALL!!
大约 11 年之前 回复
csdnceshi56
lrony* This $date = date('m/d/Y h:i:s a', time()); gets the server date but still doesn't get me the user date. This is showing me at EST when I'm at PST. I want to get the date the user sends the form and like me their server may be in a different time zone.
大约 11 年之前 回复
csdnceshi56
lrony* thanks, my service must not be up to date with php because O works for GMT but not P or "e" time zone... thanks!!
大约 11 年之前 回复
weixin_41568126
乱世@小熊 The date function is the time of your server, either check it, or temporarily set it /w PHP before you run date.
大约 11 年之前 回复
csdnceshi65
larry*wei Look into the manual: docs.php.net/manual/en/function.date.php
大约 11 年之前 回复
csdnceshi56
lrony* What I mean is the time comes back EST when I'm PST... but why? Is it the server time and the server is EST? Can I get users time? Their server may be another time zone, no?
大约 11 年之前 回复
echo date("d-m-Y H:i:sa");

This code will get the date and time of the server that the code runs on.

csdnceshi64
游.程 Additionally if you bothered to read the original post, it literally said "This code will get the date and time of your local machine (PC).". That's utterly misleading.
接近 2 年之前 回复
csdnceshi64
游.程 Did you think to look at the edit? I posted that comment, suggested an edit on the incorrect information, and in the meantime waited for it to be approved. Now it's approved and showing the correct information.
接近 2 年之前 回复
csdnceshi50
三生石@ - "This code will get the date and time of the "server" he said nothing about the "local machine" you said that. You don't have to be running your server "locally" either - you can have a server anywhere in the world set to whatever time zone you want. Your down vote was dead wrong and for no reason
接近 2 年之前 回复
csdnceshi64
游.程 Downvoted for misleading answer. No, it won't get the time of your local machine, unless you are running the server locally as well. It will get the date and time of the server.
2 年多之前 回复

If you want a different timescale, please use:

$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);

date_default_timezone_set("Asia/Calcutta");
echo date("Y/m/d H:i:s");
csdnceshi61
derek5. I believe @Jaymin may mean if you want to adjust the time ahead a day, back a month, etc.
5 年多之前 回复
csdnceshi63
elliott.david "timezone", probably.
接近 6 年之前 回复
csdnceshi74
7*4 What do you mean by "timescale"?
接近 6 年之前 回复
// Simply:
$date = date('Y-m-d H:i:s');

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

// This would return the date in the following formats respectively:
$date = '2012-03-06 17:33:07';
// Or
$date = '2012/03/06 17:33:07';

/** 
 * This time is based on the default server time zone.
 * If you want the date in a different time zone,
 * say if you come from Nairobi, Kenya like I do, you can set
 * the time zone to Nairobi as shown below.
 */

date_default_timezone_set('Africa/Nairobi');

// Then call the date functions
$date = date('Y-m-d H:i:s');
// Or
$date = date('Y/m/d H:i:s');

// date_default_timezone_set() function is however
// supported by PHP version 5.1.0 or above.

For a time-zone reference, see List of Supported Timezones.

csdnceshi68
local-host Thank you for the link of supported timezones, i was about to google it :)
大约 2 年之前 回复
csdnceshi51
旧行李 This is a much more straight-forward answer than the top one. Thanks!
大约 4 年之前 回复

You can either use the $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1.0) or the time() function to get the current Unix timestamp.

csdnceshi56
lrony* - not certain what you mean by "independent of the time zone"; it's "dependent" on the time zone the server is set to? You can have a server on the east coast set to a time zone on the west coast or vice versa.
接近 2 年之前 回复
csdnceshi80
胖鸭 It's worth noting that the timestamp returned by the time() function is independent of the timezone. (So calling date_default_timezone_set("your-particular-timezone"); before will have no effect.)
5 年多之前 回复

You can use both the $_SERVER['REQUEST_TIME'] variable or the **time()**function. Both of these return a Unix timestamp.

Most of the time these two solutions will yield the exact same Unix Timestamp. The difference between these is that $_SERVER['REQUEST_TIME'] returns the time stamp of the most recent server request and time() returns the current time. This may create minor differences in accuracy depending on your application, but for most cases both of these solutions should suffice.

Based on your example code above, you are going to want to format this information once you obtain the Unix Timestamp. An unformatted Unix timestamp looks like this...

Unix Timestamp: 1232659628

So in order to get something that will work, you can use the date() function to format it.

A good reference for ways to use the date() function is located in the PHP Manual Pages, here...

http://us.php.net/date

As an example, the following code returns a date that looks like this -

01/22/2009 04:35:00 pm

echo date("m/d/Y h:i:s a", time());
csdnceshi61
derek5. Best answer hands down. You even add how to get from UNIX timestamp to normal person date which is greatly appreciated
2 年多之前 回复
weixin_41568131
10.24 How does different web servers handle $_SERVER['REQUEST_TIME'] ?
接近 5 年之前 回复

Reference: Here's a link

This can be more reliable than simply adding or subtracting the number of seconds in a day or a month to a timestamp because of daylight saving time.

The PHP code

// 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)
csdnceshi58
Didn"t forge certainly the best ~
2 年多之前 回复

PHP's time() returns a current unix timestamp. With this, you can use the date() function to format it to your needs.

$date = date('Format String', time());

As Paolo mentioned in the comments, the second argument is redundant. The following snippet is equivalent to the one above:

$date = date('Format String');
csdnceshi80
胖鸭 the 2nd argument of the date function is assumed to be time() if left empty.
10 年多之前 回复

Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');    // MySQL datetime format
echo $now->getTimestamp();           // Unix Timestamp -- Since PHP 5.3

And to specify the timezone:

$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->getTimezone();
共30条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问