在 PHP 中可以通过date()获取当前时间,在>5.2的版本中最好还是用 datetime 类型

date()

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

DateTime

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

更完善的方法

上面两个例子返回的当前时间都是服务器时区时间(timezone 可在php.ini中声明)

Above examples will return NOW using your server timezone, as it is defined in php.ini, for example:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Athens

最准确的方法是以UTC时间,所以

/* server timezone */
define('CONST_SERVER_TIMEZONE', 'UTC'); /* server dateformat */
define('CONST_SERVER_DATEFORMAT', 'YmdHis'); <?php
/**
* Converts current time for given timezone (considering DST)
* to 14-digit UTC timestamp (YYYYMMDDHHMMSS)
*
* DateTime requires PHP >= 5.2
*
* @param $str_user_timezone
* @param string $str_server_timezone
* @param string $str_server_dateformat
* @return string
*/
function now($str_user_timezone,
$str_server_timezone = CONST_SERVER_TIMEZONE,
$str_server_dateformat = CONST_SERVER_DATEFORMAT) { // set timezone to user timezone
date_default_timezone_set($str_user_timezone); $date = new DateTime('now');
$date->setTimezone(new DateTimeZone($str_server_timezone));
$str_server_now = $date->format($str_server_dateformat); // return timezone to server default
date_default_timezone_set($str_server_timezone); return $str_server_now;
}
?>

原文 : http://www.pontikis.net/tip/?id=18

最新文章

  1. linux 学习3 第四讲 文件常用命令
  2. 03人人都应该了解的10个 jQuery 小技巧
  3. Pearson(皮尔逊)相关系数及MATLAB实现
  4. Linux 技巧:让进程在后台可靠运行的几种方法(转)
  5. (实用篇)PHP不用递归遍历目录下所有文件的代码
  6. 自定义Attribute 服务端校验 客户端校验
  7. python unittest基本介绍
  8. K最近邻算法
  9. fzu 2037 Maximum Value Problem
  10. Super Jumping! Jumping! Jumping!(dp)
  11. 14.3.4 Phantom Rows 幻影行
  12. docker 的安装
  13. (转载)app ico图标字体制作
  14. Java SE学习笔记 ---&gt;高级类特性 ---&gt; toString() 方法
  15. Python中4位1进制数与float浮点数互相转换
  16. 异常: The server time zone value &#39;&#214;&#208;&#185;&#250;&#177;&#234;&#215;&#188;&#202;&#177;&#188;&#228;&#39; is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configurat
  17. [转帖]SAP一句话入门:Plant Maintenance
  18. golang 中操作nsq队列数据库
  19. Go语言编程 (许式伟 等 著)
  20. 安全测试7_Web安全在线工具

热门文章

  1. 从C# 到 OC
  2. hdu 1166 线段树单点更新
  3. Android Inflate
  4. Codeforces Codeforces Round #316 (Div. 2) C. Replacement set
  5. 电赛菜鸟营培训(四)&mdash;&mdash;STM32F103CB之ADC转换
  6. 安卓通过putExtra传递数据的几种方式
  7. transient的理解
  8. mvc-2事件监听
  9. [BZOJ 3145][Feyat cup 1.5]Str 解题报告
  10. requirejs模块化框架用法分享