php 从php中的日期时间获取年/月/日?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7247259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Year/Month/Day from a datetime in php?
提问by Anon
I used date('w', timestamp)
and date('w', timestamp)
to know the day, date('n', timestamp)
for months, etc.
我曾经date('w', timestamp)
并且date('w', timestamp)
知道那一天,date('n', timestamp)
几个月,等等。
Now I'm using datetime and I'd like to know what are the equivalent functions to get a day, a month, etc from a datetime.
现在我正在使用日期时间,我想知道从日期时间获取一天、一个月等的等效函数是什么。
PS: I know I could use UNIX_TIMESTAMP()
in a SQL query but I prefer avoiding timestamps using in my code.
PS:我知道我可以UNIX_TIMESTAMP()
在 SQL 查询中使用,但我更喜欢避免在我的代码中使用时间戳。
回答by KingCrunch
Use DateTimewith DateTime::format()
将DateTime与DateTime::format() 一起使用
$datetime = new DateTime($dateTimeString);
echo $datetime->format('w');
回答by etuardu
Check out the manual: http://www.php.net/manual/en/datetime.format.php
查看手册:http: //www.php.net/manual/en/datetime.format.php
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>
Will output: 2000-01-01 00:00:00
将输出: 2000-01-01 00:00:00