NJ.*_*NJ. 602
时间将通过您的服务器时间.一个简单的解决方法是在调用或调用函数date_default_timezone_set
之前使用手动设置时区.date()
time()
我在墨尔本,澳大利亚所以我有这样的事情:
date_default_timezone_set('Australia/Melbourne');
Run Code Online (Sandbox Code Playgroud)
或者另一个例子是洛杉矶 - 美国:
date_default_timezone_set('America/Los_Angeles');
Run Code Online (Sandbox Code Playgroud)
您还可以通过以下方式查看服务器当前所在的时区:
date_default_timezone_get();
Run Code Online (Sandbox Code Playgroud)
所以类似于:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
Run Code Online (Sandbox Code Playgroud)
所以你的问题的简短答案是:
// Change the line below to your timezone!
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
Run Code Online (Sandbox Code Playgroud)
那么所有的时间都是你刚刚设定的时区:)
Sha*_*ina 490
// 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.
Run Code Online (Sandbox Code Playgroud)
有关时区参考,请参阅支持的时区列表.
Mah*_*hdi 183
由于PHP 5.2.0
可以用做OOP和DateTime()
也(当然如果你喜欢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
Run Code Online (Sandbox Code Playgroud)
并指定timezone
:
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); // Another way
echo $now->getTimezone();
Run Code Online (Sandbox Code Playgroud)
Sha*_*ber 87
参考:这是一个链接
由于夏令时,这比简单地将一天或一个月中的秒数添加或减去时间戳更为可靠.
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)
Run Code Online (Sandbox Code Playgroud)
Gum*_*mbo 63
您可以使用$_SERVER['REQUEST_TIME']
变量(自PHP 5.1.0起可用)或time()
函数来获取当前的Unix时间戳.
Rob*_*ska 63
PHP的time()返回当前的Unix时间戳.有了这个,您可以使用date()函数根据需要对其进行格式化.
$date = date('Format String', time());
Run Code Online (Sandbox Code Playgroud)
正如Paolo在评论中提到的,第二个论点是多余的.以下代码段与上面的代码段相同:
$date = date('Format String');
Run Code Online (Sandbox Code Playgroud)
Gri*_*are 56
您可以同时使用$_SERVER['REQUEST_TIME']
变量或time()
函数.这两个都返回一个Unix时间戳.
大多数情况下,这两个解决方案将产生完全相同的Unix时间戳.它们之间的区别在于$_SERVER['REQUEST_TIME']
返回最新服务器请求的时间戳并time()
返回当前时间.根据您的应用,这可能会在准确性方面产生细微差别,但对于大多数情况,这两种解决方案都应该足够.
根据上面的示例代码,一旦获得Unix时间戳,您将要格式化此信息.未格式化的Unix时间如下所示:1232659628
因此,为了获得可行的date()
功能,您可以使用该功能对其进行格式化.
有关使用该date()
函数的方法的一个很好的参考资料位于PHP手册中.
例如,以下代码返回如下所示的日期01/22/2009 04:35:00 pm
:
echo date("m/d/Y h:i:s a", time());
Run Code Online (Sandbox Code Playgroud)
Sub*_*axe 45
PHP的日期函数可以完成这项工作.
描述:
string date(string $format [, int $timestamp = time()])
Run Code Online (Sandbox Code Playgroud)
使用给定的整数时间戳返回根据给定格式字符串格式化的字符串,如果没有给出时间戳,则返回当前时间.
例子:
$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)
Run Code Online (Sandbox Code Playgroud)
Kai*_*las 25
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('d-m-Y H:i:s');
Run Code Online (Sandbox Code Playgroud)
更新
//Also get am/pm in datetime:
echo $date->format('d-m-Y H:i:s a'); // output 30-12-2013 10:16:15 am
Run Code Online (Sandbox Code Playgroud)
对于日期格式,PHP date()函数很有用.
小智 15
您也可以使用以下格式:
$date = date("d-m-Y");
Run Code Online (Sandbox Code Playgroud)
要么
$date = date("Y-m-d H:i:s");
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 14
根据PHP如何获取当前日期时间(NOW),有两种常用方法可以获取当前日期.要使用PHP获取当前日期时间(现在),您可以将该date
类与任何PHP版本一起使用,或者更好地datetime
使用PHP> = 5.2 的类.
这里提供了各种日期格式表达式.
此表达式将以格式返回NOW Y-m-d H:i:s
.
<?php
echo date('Y-m-d H:i:s');
?>
Run Code Online (Sandbox Code Playgroud)
此表达式将以格式返回NOW Y-m-d H:i:s
.
<?php
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
?>
Run Code Online (Sandbox Code Playgroud)
AMB*_*AMB 13
<?php
echo "<b>".date('l\, F jS\, Y ')."</b>";
?>
Run Code Online (Sandbox Code Playgroud)
打印像这样
2012年12月9日星期日
Mr.*_* HK 12
<?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)
?>
Run Code Online (Sandbox Code Playgroud)
Sup*_*eth 12
对于新的PHP程序员,可能会感到困惑,为什么有很多方法可以获取当前日期和时间,以及在项目中使用哪种方法。
1. date
方法(PHP 4,PHP 5,PHP 7)
这是在php中获取日期和时间的最常见,最简单的方法。
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
Run Code Online (Sandbox Code Playgroud)
您可以在这里了解更多
2. DateTime
类(PHP 5> = 5.2.0,PHP 7)
当您想将PHP与OOP一起使用时,这是获取日期和时间的最佳方法。
<?php
// Specified date/time in your computer's time zone.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Specified date/time in the specified time zone.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in your computer's time zone.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Using a UNIX timestamp. Notice the result is in the UTC time zone.
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Run Code Online (Sandbox Code Playgroud)
您可以在这里了解更多
3.碳日期时间包
如果您使用的是Composer,Laravel,Symfony或任何种类的框架,则这是获取日期和时间的最佳方法。另外,此包在php中扩展了DateTime类,因此您可以使用Datetime类中的所有方法。像laravel这样的内置框架,因此您不必单独安装。
printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); // automatically converted to string
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
// Carbon embed 823 languages:
echo $tomorrow->locale('fr')->isoFormat('dddd, MMMM Do YYYY, h:mm');
echo $tomorrow->locale('ar')->isoFormat('dddd, MMMM Do YYYY, h:mm');
$officialDate = Carbon::now()->toRfc2822String();
$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
$internetWillBlowUpOn = Carbon::create(2038, 01, 19, 3, 14, 7, 'GMT');
if (Carbon::now()->isWeekend()) {
echo 'Party!';
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
Run Code Online (Sandbox Code Playgroud)
您可以在这里了解更多
希望这会有所帮助,如果您知道其他获取日期和时间的方法,请随时编辑答案。
kka*_*yat 11
设置您的时区:
date_default_timezone_set('Asia/Calcutta');
Run Code Online (Sandbox Code Playgroud)
然后调用日期函数
$date = date('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)
Joo*_*ler 11
date(format, timestamp)
Run Code Online (Sandbox Code Playgroud)
该date
函数使用给定的整数时间戳返回根据给定格式字符串格式化的字符串,如果没有给出时间戳,则返回当前时间.换句话说,timestamp
是可选的,默认为time()的值.
参数是 -
格式 - 必填.指定时间戳的格式
timestamp - (可选)指定时间戳.默认值是当前日期和时间
date()
函数所需的格式参数指定如何格式化date (or time)
.
以下是一些常用于日期的字符:
其他字符"/", ".", or "-"
也可以在字符之间插入以添加其他格式.
下面的示例以三种不同的方式格式化今天的日期:
<?php
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");
?>
Run Code Online (Sandbox Code Playgroud)
Jay*_*min 10
如果您想要不同的时间表,请使用:
$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");
Run Code Online (Sandbox Code Playgroud)
Dar*_*z J 10
date_default_timezone_set('Europe/Warsaw');
echo("<p class='time'>".date('H:i:s')."</p>");
echo("<p class='date'>".date('d/m/Y')."</p>");
Run Code Online (Sandbox Code Playgroud)
小智 10
日期格式也取决于:
echo date("d/m/Y H:i:sa"); // 13/04/2017 19:38:15pm
Run Code Online (Sandbox Code Playgroud)
小智 10
您可以使用此代码:
<?php
$currentDateTime = date('Y-m-d H:i:s');
echo $currentDateTime;
?>
Run Code Online (Sandbox Code Playgroud)
我发现在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');
Run Code Online (Sandbox Code Playgroud)
另一种简单的方法是获取当前日期和时间的时间戳.使用mktime()函数:
$now = mktime(); // Return timestamp of the current time
Run Code Online (Sandbox Code Playgroud)
然后您可以将其转换为另一种日期格式:
//// Prints something like: Thursday 26th of January 2017 01:12:36 PM
echo date('l jS \of F Y h:i:s A',$now);
Run Code Online (Sandbox Code Playgroud)
更多日期格式在这里:http: //php.net/manual/en/function.date.php
您所在国家/地区的时区:支持的时区列表
date_default_timezone_set('Asia/Kolkata');
$dateYmd = date('Y-m-d');
echo "Current Year Month Day: $dateYmd";
Run Code Online (Sandbox Code Playgroud)
本年月日:2022-01-03
$datehms = date('h:i:s');
echo "Current Hour Minute Second: $datehms";
Run Code Online (Sandbox Code Playgroud)
当前时分秒:11:05:38
很简单的
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y H:i:s', time());
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用此代码来获取当前日期和时间
echo date('r', time());
Run Code Online (Sandbox Code Playgroud)
或者使用它来进行更多定制。
echo date('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)
// Set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2016 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2016 is on a Saturday
echo "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 -0700
echo date(DATE_RFC2822);
// Prints something like: 2016-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
Run Code Online (Sandbox Code Playgroud)
如果你是孟加拉国人,如果你想得到达卡的时间,那么使用这个:
$date = new DateTime();
$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));
$get_datetime = $date->format('d.m.Y H:i:s');
Run Code Online (Sandbox Code Playgroud)
小智 6
通常,这个日期函数对每个人都很有用:date("Y/m/d");
但时间是不同的,因为时间函数取决于 PHP 版本或系统日期。
所以可能像这样使用它来获取我们自己的时区:
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('H:m:s');
Run Code Online (Sandbox Code Playgroud)
此功能显示 24 小时制时间。
小智 6
以下是一些常用的时间字符:
d - 代表月份中的日期(01 到 31)
m - 代表一个月(01 到 12)
Y - 代表年份(四位数字)
l(小写“L”)- 代表星期几
H - 24 小时格式的小时(00 到 23)
h - 12 小时格式的一小时,带前导零(01 到 12)
i - 带有前导零的分钟(00 到 59)
s - 带有前导零的秒(00 到 59)
a - 小写的午前和午后(上午或下午)
例子 :
echo "Today " . date("Y/m/d") ;
echo "time " . date("h:i:sa");
Run Code Online (Sandbox Code Playgroud)
以下是一些常用的字符:
a - 小写Ante meridiem和Post meridiem(上午或下午)
获取您的时区
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
Run Code Online (Sandbox Code Playgroud)
检查出来(可选)
<?php
$d = mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
Run Code Online (Sandbox Code Playgroud)
约会
<?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");
?>
Run Code Online (Sandbox Code Playgroud)
以下是一些常用于日期的字符:
小智 5
获取当前时间和日期的最佳方法是通过PHP 中的date 函数:
$date = date('FORMAT'); // FORMAT E.g.: Y-m-d H:i:s
$current_date = date('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)
使用 Unix 时间戳:
$now_date = date('FORMAT', time()); // FORMAT Eg : Y-m-d H:i:s
Run Code Online (Sandbox Code Playgroud)
设置服务器时区:
date_default_timezone_set('Asia/Calcutta');
Run Code Online (Sandbox Code Playgroud)
不同的时区列表在这里。
只需使用: date("Y-m-d H:i:s")
这将为您提供日期和时间,例如 '2020-08-22 12:20:30' this 。date_default_timezone_set("your time zone")
在 date() 函数之前添加以获取您所在区域/区域的时间日期。
在这里你可以找到你的时区