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

时间:2022-10-28 22:46:30

Which PHP function can return the current date/time?

哪个PHP函数可以返回当前日期/时间?

31 个解决方案

#1


517  

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.

时间会在你的服务器时间。一个简单的解决方法是,在调用date()或time()函数之前,使用date_default_timezone_set手动设置时区。

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

我在澳大利亚的墨尔本,所以我有这样的东西:

date_default_timezone_set('Australia/Melbourne');

Or another example is LA - US:

另一个例子是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 :)

那么所有的时间都是在你刚刚设置的时区:)

#2


372  

    // 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.

有关时区引用,请参见支持的时区列表。

#3


158  

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

因为PHP 5.2.0也可以使用OOP和DateTime()(当然,如果您喜欢OOP):

$now = new DateTime();echo $now->format('Y-m-d H:i:s');    // MySQL datetime formatecho $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 wayecho $now->getTimezone();

#4


74  

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

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)

#5


56  

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.

您可以使用$_SERVER['REQUEST_TIME']变量(自PHP 5.1.0以来可用)或time()函数来获取当前的Unix时间戳。

#6


56  

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

PHP的time()返回当前的unix时间戳。有了这个,您可以使用date()函数将它格式化为您需要的格式。

$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:

正如Paolo在评论中提到的,第二个论点是多余的。以下片段相当于上面的片段:

$date = date('Format String');

#7


43  

PHP's date function can do this job

PHP的date函数可以完成这项工作

date()

日期()

Description :

描述:

string date ( string $format [, int $timestamp = time() ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

根据给定的格式返回一个字符串格式化字符串使用给定的整数时间戳或当前时间如果没有时间戳。

Examples :

例子:

$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)

#8


38  

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

您可以同时使用$_SERVER['REQUEST_TIME']变量或**time()**函数。这两种方法都返回一个Unix时间戳。

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.

在大多数情况下,这两个解决方案会产生完全相同的Unix时间戳。它们之间的区别是$_SERVER['REQUEST_TIME']返回最近的服务器请求的时间戳,而time()返回当前时间。这可能会在准确性上产生微小的差异,这取决于您的应用程序,但是对于大多数情况,这两种解决方案都应该足够了。

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时间戳,您将希望格式化此信息。未格式化的Unix时间戳如下所示……

Unix Timestamp: 1232659628

Unix时间戳:1232659628

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

因此,为了得到能够工作的东西,可以使用date()函数来格式化它。

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

使用date()函数的一个很好的参考是位于PHP手册页中,这里……

http://us.php.net/date

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

01/22/2009 04:35:00点

echo date("m/d/Y h:i:s a", time());

#9


22  

 $date = new DateTime('now', new DateTimeZone('Asia/Kolkata')); echo $date->format('d-m-Y H:i:s');

Update

更新

 //Also get am/pm in datetime: echo $date->format('d-m-Y H:i:s a'); // output 30-12-2013 10:16:15 am

For the date format, PHP date() Function is useful.

对于日期格式,PHP date()函数是有用的。

#10


18  

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

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

工作,但怎么也知道美国东部时间,PST吗?

#11


16  

echo date("d-m-Y H:i:sa");

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

该代码将获得代码运行的服务器的日期和时间。

#12


13  

According to the article How to Get Current Datetime (NOW) with PHP, there are two common ways to get the current date. To get current datetime (now) with PHP, you can use the date class with any PHP version, or better the datetime class with PHP >= 5.2.

根据如何使用PHP获取当前日期时间(NOW)的文章,有两种常见的方法获得当前日期。要使用PHP获取当前的datetime(现在),您可以使用任何PHP版本的date类,或者使用PHP >= 5.2更好的datetime类。

Various date format expressions are available here.

这里有各种日期格式表达式。

Example using date

This expression will return NOW in format Y-m-d H:i:s.

这个表达式现在将以格式Y-m-d H:i:s返回。

<?php    echo date('Y-m-d H:i:s');?>

Example using datetime class

This expression will return NOW in format Y-m-d H:i:s.

这个表达式现在将以格式Y-m-d H:i:s返回。

<?php    $dt = new DateTime();    echo $dt->format('Y-m-d H:i:s');?>

#13


13  

You can use this format also

您也可以使用这种格式

$date = date("d-m-Y");

or

$date = date("Y-m-d H:i:s"); 

#14


12  

<?phpecho "<b>".date('l\, F jS\, Y ')."</b>";?>

Prints like this

像这样的照片

Sunday, December 9th, 2012

周日,2012年12月9日

#15


11  

<?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)?>

#16


11  

its very simple

它非常简单

echo $date = date('Y-m-d H:i:s');

echo $date = date('Y-m-d H:i:s');

#17


9  

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");

#18


9  

Set your time zone:

设置您的时区:

date_default_timezone_set('Asia/Calcutta');

Then call the date functions

然后调用date函数

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

#19


9  

date_default_timezone_set('Europe/Warsaw');echo("<p class='time'>".date('H:i:s')."</p>");echo("<p class='date'>".date('d/m/Y')."</p>");

#20


9  

date(format,timestamp)

Date Function returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

Date函数返回一个字符串,该字符串根据给定的格式字符串使用给定的整数时间戳进行格式化,如果没有给定时间戳,则返回当前时间。换句话说,timestamp是可选的,默认值是time()。

And the parameters are -

参数是-

format - Required. Specifies the format of the timestamp

格式要求。指定时间戳的格式

timestamp - (Optional) Specifies a timestamp. Default is the current date and time

时间戳——(可选)指定时间戳。默认是当前日期和时间

How to get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).

函数的required format参数指定如何格式化日期(或时间)。

Here are some characters that are commonly used for dates:

以下是一些常用的日期:

  1. d - Represents the day of the month (01 to 31)
  2. d -表示月份的日期(01至31)
  3. m - Represents a month (01 to 12)
  4. m -表示一个月(01到12)
  5. Y - Represents a year (in four digits)
  6. Y -表示一年(四位数)
  7. l (lowercase 'L') - Represents the day of the week
  8. l(小写的“l”)表示一周的一天

Other characters, like "/", ".", or "-" can also be inserted between the characters to add additional formatting.

其他字符,如"/","也可以在字符之间插入“-”,以添加其他格式。

The example below formats today's date in three different ways:

下面的例子以三种不同的方式格式化了今天的日期:

<?phpecho "Today is " . date("Y/m/d") . "<br>";echo "Today is " . date("Y.m.d") . "<br>";echo "Today is " . date("Y-m-d") . "<br>";echo "Today is " . date("l");?>

Some Useful Links

  • gmdate() - Format aGMT/UTC date/time
  • gmdate() -格式化aGMT/UTC日期/时间
  • idate() - Format alocal time/date as integer
  • idate() -格式化整数时间/日期
  • getdate() - Getdate/time information
  • 获取当前日期()——获取当前日期/时间信息
  • getlastmod() -Gets time of last page modification
  • 获取最后页面修改的时间
  • mktime() - Get Unixtimestamp for a date
  • mktime() -获取日期的Unixtimestamp
  • strftime() - Formata local time/date according to locale settings
  • 根据地区设置,本地时间/日期。
  • time() - Return currentUnix timestamp
  • time()——返回currentUnix时间戳
  • strtotime() -Parse about any English textual datetime description into a Unixtimestamp
  • strtotime() -将任何英语文本日期时间描述解析为Unixtimestamp
  • Predefined DateTimeConstants
  • 预定义DateTimeConstants

#21


9  

Date Format depends too :

日期格式也取决于:

echo date("d/m/Y H:i:sa");    // 13/04/2017 19:38:15pm

#22


9  

You can use this code.

您可以使用此代码。

  <?php  $currentDateTime = date('Y-m-d H:i:s');  echo $currentDateTime;  ?>

#23


7  

I found that the simplest way of getting the current time in PHP is something like this.

我发现在PHP中获取当前时间最简单的方法是这样的。

//Prints out something like 10:00am Just be sure to set your timezone correctly.date_default_timezone_set("America/Chicago");$TIME = date('G:ia'); 

#24


7  

Another simple way to take timestamp of current date and time use mktime() function

使用mktime()函数获取当前日期和时间戳的另一种简单方法

$now = mktime(); //return timestamp of current time

then you can convert this to another date format:

然后你可以把它转换成另一种日期格式:

//// Prints something like: Thursday 26th of January 2017 01:12:36 PMecho date('l jS \of F Y h:i:s A',$now);

more date formats here:http://php.net/manual/en/function.date.php

更多的日期格式:http://php.net/manual/en/function.date.php

#25


5  

// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC');// Prints something like: Mondayecho date("l");// Prints something like: Monday 8th of August 2016 03:12:46 PMecho date('l jS \of F Y h:i:s A');// Prints: July 1, 2016 is on a Saturdayecho "July 1, 2016 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2016));/* use the constants in the format parameter */// prints something like: Wed, 25 Sep 2013 15:28:57 -0700echo date(DATE_RFC2822);// prints something like: 2016-07-01T00:00:00+00:00echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

#26


5  

If you are Bangladeshi. And if you want get time of Dhaka then use it

如果你是孟加拉国。如果你想知道达卡的时间,那就用它

$date = new DateTime();$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));$get_datetime = $date->format('d.m.Y H:i:s');

#27


5  

That's May Help you.

这可能会帮助你。

Here are some characters that are commonly used for times:

以下是一些常用的时间符号:

  1. h - 12-hour format of an hour with leading zeros (01 to 12)
  2. h - 12小时格式,以0开头(01至12)
  3. i - Minutes with leading zeros (00 to 59)
  4. i - 0到59分钟
  5. s - Seconds with leading zeros (00 to 59)
  6. s -领先0秒(00到59)
  7. a - Lowercase Ante meridiem and Post meridiem (am or pm)

    a -小写的前分生和后分生(上午或下午)

Get Your Time Zone

让你的时区

<?phpdate_default_timezone_set("America/New_York");echo "The time is " . date("h:i:sa");?>

check this out (optional)

看看这个(可选)

<?php    $d=mktime(11, 14, 54, 8, 12, 2014);    echo "Created date is " . date("Y-m-d h:i:sa", $d);    ?>

for Date

为日期

<?php    echo "Today is " . date("Y/m/d") . ;    echo "Today is " . date("Y.m.d") . ;    echo "Today is " . date("Y-m-d") . ;    echo "Today is " . date("l");?>

Here are some characters that are commonly used for dates:

以下是一些常用的日期:

  1. d - Represents the day of the month (01 to 31)
  2. d -表示月份的日期(01至31)
  3. m - Represents a month (01 to 12)
  4. m -表示一个月(01到12)
  5. Y - Represents a year (in four digits)
  6. Y -表示一年(四位数)
  7. l (lowercase 'L') - Represents the day of the week
  8. l(小写的“l”)表示一周的一天

Source-W3-Schools

Source-W3-Schools

#28


4  

If you want to get Date like 12-3-2016 separately each day,month and year values, then copy-paste this code

如果您希望每天、每月和每年分别获得12-3-2016这样的日期,那么复制粘贴此代码

$day=date("d");$month=date("m");$year=date("y");print "date".$day."-".$month."-".$year;

#29


3  

 We can use date function and set default timezone. <?php  date_default_timezone_set("Asia/Kolkata");  echo "Today is " . date("Y/m/d") . "<br>";  echo "Today is " . date("Y.m.d") . "<br>";  echo "Today is " . date("Y-m-d") . "<br>";  echo "Today is " . date("l");  echo "The time is " . date("h:i:sa"); ?>

#30


2  

PHP returns current time in seconds you need to format them in whatever format you want

PHP以秒为单位返回当前时间,您需要将它们格式化为任何您想要的格式

<?php//time() returns current time in seconds$in_seconds = time();//strftime - Format a local time/date according to locale settingsecho strftime("%m/%d/%y",$in_seconds);?>

Reference: http://php.net/manual/en/function.strftime.php

参考:http://php.net/manual/en/function.strftime.php

#1


517  

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.

时间会在你的服务器时间。一个简单的解决方法是,在调用date()或time()函数之前,使用date_default_timezone_set手动设置时区。

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

我在澳大利亚的墨尔本,所以我有这样的东西:

date_default_timezone_set('Australia/Melbourne');

Or another example is LA - US:

另一个例子是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 :)

那么所有的时间都是在你刚刚设置的时区:)

#2


372  

    // 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.

有关时区引用,请参见支持的时区列表。

#3


158  

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

因为PHP 5.2.0也可以使用OOP和DateTime()(当然,如果您喜欢OOP):

$now = new DateTime();echo $now->format('Y-m-d H:i:s');    // MySQL datetime formatecho $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 wayecho $now->getTimezone();

#4


74  

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

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)

#5


56  

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.

您可以使用$_SERVER['REQUEST_TIME']变量(自PHP 5.1.0以来可用)或time()函数来获取当前的Unix时间戳。

#6


56  

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

PHP的time()返回当前的unix时间戳。有了这个,您可以使用date()函数将它格式化为您需要的格式。

$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:

正如Paolo在评论中提到的,第二个论点是多余的。以下片段相当于上面的片段:

$date = date('Format String');

#7


43  

PHP's date function can do this job

PHP的date函数可以完成这项工作

date()

日期()

Description :

描述:

string date ( string $format [, int $timestamp = time() ] )

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

根据给定的格式返回一个字符串格式化字符串使用给定的整数时间戳或当前时间如果没有时间戳。

Examples :

例子:

$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)

#8


38  

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

您可以同时使用$_SERVER['REQUEST_TIME']变量或**time()**函数。这两种方法都返回一个Unix时间戳。

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.

在大多数情况下,这两个解决方案会产生完全相同的Unix时间戳。它们之间的区别是$_SERVER['REQUEST_TIME']返回最近的服务器请求的时间戳,而time()返回当前时间。这可能会在准确性上产生微小的差异,这取决于您的应用程序,但是对于大多数情况,这两种解决方案都应该足够了。

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时间戳,您将希望格式化此信息。未格式化的Unix时间戳如下所示……

Unix Timestamp: 1232659628

Unix时间戳:1232659628

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

因此,为了得到能够工作的东西,可以使用date()函数来格式化它。

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

使用date()函数的一个很好的参考是位于PHP手册页中,这里……

http://us.php.net/date

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

01/22/2009 04:35:00点

echo date("m/d/Y h:i:s a", time());

#9


22  

 $date = new DateTime('now', new DateTimeZone('Asia/Kolkata')); echo $date->format('d-m-Y H:i:s');

Update

更新

 //Also get am/pm in datetime: echo $date->format('d-m-Y H:i:s a'); // output 30-12-2013 10:16:15 am

For the date format, PHP date() Function is useful.

对于日期格式,PHP date()函数是有用的。

#10


18  

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

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

工作,但怎么也知道美国东部时间,PST吗?

#11


16  

echo date("d-m-Y H:i:sa");

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

该代码将获得代码运行的服务器的日期和时间。

#12


13  

According to the article How to Get Current Datetime (NOW) with PHP, there are two common ways to get the current date. To get current datetime (now) with PHP, you can use the date class with any PHP version, or better the datetime class with PHP >= 5.2.

根据如何使用PHP获取当前日期时间(NOW)的文章,有两种常见的方法获得当前日期。要使用PHP获取当前的datetime(现在),您可以使用任何PHP版本的date类,或者使用PHP >= 5.2更好的datetime类。

Various date format expressions are available here.

这里有各种日期格式表达式。

Example using date

This expression will return NOW in format Y-m-d H:i:s.

这个表达式现在将以格式Y-m-d H:i:s返回。

<?php    echo date('Y-m-d H:i:s');?>

Example using datetime class

This expression will return NOW in format Y-m-d H:i:s.

这个表达式现在将以格式Y-m-d H:i:s返回。

<?php    $dt = new DateTime();    echo $dt->format('Y-m-d H:i:s');?>

#13


13  

You can use this format also

您也可以使用这种格式

$date = date("d-m-Y");

or

$date = date("Y-m-d H:i:s"); 

#14


12  

<?phpecho "<b>".date('l\, F jS\, Y ')."</b>";?>

Prints like this

像这样的照片

Sunday, December 9th, 2012

周日,2012年12月9日

#15


11  

<?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)?>

#16


11  

its very simple

它非常简单

echo $date = date('Y-m-d H:i:s');

echo $date = date('Y-m-d H:i:s');

#17


9  

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");

#18


9  

Set your time zone:

设置您的时区:

date_default_timezone_set('Asia/Calcutta');

Then call the date functions

然后调用date函数

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

#19


9  

date_default_timezone_set('Europe/Warsaw');echo("<p class='time'>".date('H:i:s')."</p>");echo("<p class='date'>".date('d/m/Y')."</p>");

#20


9  

date(format,timestamp)

Date Function returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

Date函数返回一个字符串,该字符串根据给定的格式字符串使用给定的整数时间戳进行格式化,如果没有给定时间戳,则返回当前时间。换句话说,timestamp是可选的,默认值是time()。

And the parameters are -

参数是-

format - Required. Specifies the format of the timestamp

格式要求。指定时间戳的格式

timestamp - (Optional) Specifies a timestamp. Default is the current date and time

时间戳——(可选)指定时间戳。默认是当前日期和时间

How to get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).

函数的required format参数指定如何格式化日期(或时间)。

Here are some characters that are commonly used for dates:

以下是一些常用的日期:

  1. d - Represents the day of the month (01 to 31)
  2. d -表示月份的日期(01至31)
  3. m - Represents a month (01 to 12)
  4. m -表示一个月(01到12)
  5. Y - Represents a year (in four digits)
  6. Y -表示一年(四位数)
  7. l (lowercase 'L') - Represents the day of the week
  8. l(小写的“l”)表示一周的一天

Other characters, like "/", ".", or "-" can also be inserted between the characters to add additional formatting.

其他字符,如"/","也可以在字符之间插入“-”,以添加其他格式。

The example below formats today's date in three different ways:

下面的例子以三种不同的方式格式化了今天的日期:

<?phpecho "Today is " . date("Y/m/d") . "<br>";echo "Today is " . date("Y.m.d") . "<br>";echo "Today is " . date("Y-m-d") . "<br>";echo "Today is " . date("l");?>

Some Useful Links

  • gmdate() - Format aGMT/UTC date/time
  • gmdate() -格式化aGMT/UTC日期/时间
  • idate() - Format alocal time/date as integer
  • idate() -格式化整数时间/日期
  • getdate() - Getdate/time information
  • 获取当前日期()——获取当前日期/时间信息
  • getlastmod() -Gets time of last page modification
  • 获取最后页面修改的时间
  • mktime() - Get Unixtimestamp for a date
  • mktime() -获取日期的Unixtimestamp
  • strftime() - Formata local time/date according to locale settings
  • 根据地区设置,本地时间/日期。
  • time() - Return currentUnix timestamp
  • time()——返回currentUnix时间戳
  • strtotime() -Parse about any English textual datetime description into a Unixtimestamp
  • strtotime() -将任何英语文本日期时间描述解析为Unixtimestamp
  • Predefined DateTimeConstants
  • 预定义DateTimeConstants

#21


9  

Date Format depends too :

日期格式也取决于:

echo date("d/m/Y H:i:sa");    // 13/04/2017 19:38:15pm

#22


9  

You can use this code.

您可以使用此代码。

  <?php  $currentDateTime = date('Y-m-d H:i:s');  echo $currentDateTime;  ?>

#23


7  

I found that the simplest way of getting the current time in PHP is something like this.

我发现在PHP中获取当前时间最简单的方法是这样的。

//Prints out something like 10:00am Just be sure to set your timezone correctly.date_default_timezone_set("America/Chicago");$TIME = date('G:ia'); 

#24


7  

Another simple way to take timestamp of current date and time use mktime() function

使用mktime()函数获取当前日期和时间戳的另一种简单方法

$now = mktime(); //return timestamp of current time

then you can convert this to another date format:

然后你可以把它转换成另一种日期格式:

//// Prints something like: Thursday 26th of January 2017 01:12:36 PMecho date('l jS \of F Y h:i:s A',$now);

more date formats here:http://php.net/manual/en/function.date.php

更多的日期格式:http://php.net/manual/en/function.date.php

#25


5  

// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC');// Prints something like: Mondayecho date("l");// Prints something like: Monday 8th of August 2016 03:12:46 PMecho date('l jS \of F Y h:i:s A');// Prints: July 1, 2016 is on a Saturdayecho "July 1, 2016 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2016));/* use the constants in the format parameter */// prints something like: Wed, 25 Sep 2013 15:28:57 -0700echo date(DATE_RFC2822);// prints something like: 2016-07-01T00:00:00+00:00echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

#26


5  

If you are Bangladeshi. And if you want get time of Dhaka then use it

如果你是孟加拉国。如果你想知道达卡的时间,那就用它

$date = new DateTime();$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));$get_datetime = $date->format('d.m.Y H:i:s');

#27


5  

That's May Help you.

这可能会帮助你。

Here are some characters that are commonly used for times:

以下是一些常用的时间符号:

  1. h - 12-hour format of an hour with leading zeros (01 to 12)
  2. h - 12小时格式,以0开头(01至12)
  3. i - Minutes with leading zeros (00 to 59)
  4. i - 0到59分钟
  5. s - Seconds with leading zeros (00 to 59)
  6. s -领先0秒(00到59)
  7. a - Lowercase Ante meridiem and Post meridiem (am or pm)

    a -小写的前分生和后分生(上午或下午)

Get Your Time Zone

让你的时区

<?phpdate_default_timezone_set("America/New_York");echo "The time is " . date("h:i:sa");?>

check this out (optional)

看看这个(可选)

<?php    $d=mktime(11, 14, 54, 8, 12, 2014);    echo "Created date is " . date("Y-m-d h:i:sa", $d);    ?>

for Date

为日期

<?php    echo "Today is " . date("Y/m/d") . ;    echo "Today is " . date("Y.m.d") . ;    echo "Today is " . date("Y-m-d") . ;    echo "Today is " . date("l");?>

Here are some characters that are commonly used for dates:

以下是一些常用的日期:

  1. d - Represents the day of the month (01 to 31)
  2. d -表示月份的日期(01至31)
  3. m - Represents a month (01 to 12)
  4. m -表示一个月(01到12)
  5. Y - Represents a year (in four digits)
  6. Y -表示一年(四位数)
  7. l (lowercase 'L') - Represents the day of the week
  8. l(小写的“l”)表示一周的一天

Source-W3-Schools

Source-W3-Schools

#28


4  

If you want to get Date like 12-3-2016 separately each day,month and year values, then copy-paste this code

如果您希望每天、每月和每年分别获得12-3-2016这样的日期,那么复制粘贴此代码

$day=date("d");$month=date("m");$year=date("y");print "date".$day."-".$month."-".$year;

#29


3  

 We can use date function and set default timezone. <?php  date_default_timezone_set("Asia/Kolkata");  echo "Today is " . date("Y/m/d") . "<br>";  echo "Today is " . date("Y.m.d") . "<br>";  echo "Today is " . date("Y-m-d") . "<br>";  echo "Today is " . date("l");  echo "The time is " . date("h:i:sa"); ?>

#30


2  

PHP returns current time in seconds you need to format them in whatever format you want

PHP以秒为单位返回当前时间,您需要将它们格式化为任何您想要的格式

<?php//time() returns current time in seconds$in_seconds = time();//strftime - Format a local time/date according to locale settingsecho strftime("%m/%d/%y",$in_seconds);?>

Reference: http://php.net/manual/en/function.strftime.php

参考:http://php.net/manual/en/function.strftime.php