哪个PHP函数可以返回当前日期/时间?
当前回答
PHP以秒为单位返回当前时间。你需要把它们格式化成你想要的任何格式。
<?php
// time() returns current time in seconds
$in_seconds = time();
// strftime - Format a local time/date according to locale settings
echo strftime("%m/%d/%y", $in_seconds);
?>
参考:http://php.net/manual/en/function.strftime.php
其他回答
您可以使用$_SERVER['REQUEST_TIME']变量(自PHP 5.1.0起可用)或time()函数来获取当前的Unix时间戳。
下面是一些常用的表示时间的字符:
H -以前导零表示的12小时格式(01到12) i -前导0的分钟(00到59) s -前导0的秒(00到59) a -小写的子午线前后(am或pm)
确定你的时区
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
看看这个(可选)
<?php
$d = mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
为日期
<?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");
?>
下面是一些常用来表示日期的字符:
d -表示一个月中的第一天(01 ~ 31) m -代表一个月(01 ~ 12) Y -年份(四位数) l(小写“l”)-代表星期几
Source-W3-Schools
// 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.
有关时区参考,请参见支持的时区列表。
如果你想获得日期,如12-3-2016,分开每一天,月和年的值,然后复制粘贴以下代码:
$day = date("d");
$month = date("m");
$year = date("y");
print "date" . $day . "-" . $month . "-" . $year;
你也可以使用这种格式:
$date = date("d-m-Y");
Or
$date = date("Y-m-d H:i:s");
推荐文章
- 如何从一个查询插入多行使用雄辩/流利
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 在mongodb中存储日期/时间的最佳方法
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- 在Android应用程序中显示当前时间和日期
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- PHP中的echo, print和print_r有什么区别?
- 如何转换日期时间?将日期时间
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 从IP地址获取位置
- 在c#中创建一个特定时区的DateTime
- 获取数组值的键名