php 从 MYSQL 上的时间戳中提取日/月/年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2900743/
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
Extract the Day / Month / Year from a Timestamp on MYSQL
提问by sf_tristanb
I have :
我有 :
$date = $actualite['date'];
$actualite['date']is a TIMESTAMP
$actualite['date']是时间戳
And I was wondering how can I extract from this timestampthe day, then the month, then the year in 3 variables.
我想知道如何从这个时间戳中提取3 个变量中的日期、月份和年份。
Thank you for your help :)
感谢您的帮助 :)
回答by Andy E
Use date_parse($actualite['date']);, which will return an array containing the day, month, year and other items.
使用date_parse($actualite['date']);,它将返回一个包含日、月、年和其他项目的数组。
http://www.php.net/manual/en/function.date-parse.php
http://www.php.net/manual/en/function.date-parse.php
Example:
例子:
<?php
print_r(date_parse("2006-12-12 10:00:00.5"));
?>
Output:
输出:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
回答by Mark Baker
You can extract the values directly within your MySQL query
您可以直接在 MySQL 查询中提取值
SELECT DAY( <TIMESTAMP_FIELD> ) AS DAY,
MONTH( <TIMESTAMP_FIELD> ) AS MONTH,
YEAR( <TIMESTAMP_FIELD> ) AS YEAR
FROM <TABLE>
回答by Patrick Münster
Another way with more options for formatting would be:
具有更多格式选项的另一种方法是:
$date = date_create($myTimeStamp); // From database "2020-04-09 17:59:20"
$formatedDate = date_format($date, "d/m/y"); // --> 09/04/20
https://www.php.net/manual/en/datetime.format.php
https://www.php.net/manual/en/datetime.format.php
It might be less intuitive than date_parse()but gives you more options as far as I can see.
据date_parse()我所知,它可能不那么直观,但为您提供了更多选择。

