php 使用php从时间戳中获取时间和日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9970584/
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
Getting time and date from timestamp with php
提问by Hubert
in my database I have a time stamp column...which reflects a format like this: 2012-04-02 02:57:54
在我的数据库中,我有一个时间戳列......它反映了这样的格式:2012-04-02 02:57:54
However I would like to separate them up into $date
and $time
.
但是我想将它们分成$date
和$time
。
after some research through the php manual...I found that date() , date_format() and strtotime() are able to help me to separate them...(not sure if I am right)
经过php手册的一些研究......我发现 date() , date_format() 和 strtotime() 能够帮助我将它们分开......(不确定我是否正确)
but I not very sure of how to code it out...
但我不太确定如何编码...
In my php file...the timestamp extracted would be $row['DATETIMEAPP'].
在我的 php 文件中...提取的时间戳将是 $row['DATETIMEAPP']。
Will
将要
$date= strtotime('d-m-Y',$row['DATETIMEAPP']);
$time= strtotime('Gi.s',$row['DATETIMEAPP']);
or
或者
$date= date('d-m-Y',$row['DATETIMEAPP']);
work?
工作?
Can i use date() to get the time as well??
我也可以使用 date() 来获取时间吗??
Thanks in advance
提前致谢
回答by Andreas Wong
$timestamp = strtotime($row['DATETIMEAPP']);
gives you timestamp, which then you can use date to format:
给你时间戳,然后你可以使用日期来格式化:
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
Alternatively
或者
list($date, $time) = explode('|', date('d-m-Y|Gi.s', $timestamp));
回答by Ankit Bansal
If you dont want to change the format of date and time from the timestamp, you can use the explode
function in php
如果你不想从时间戳改变日期和时间的格式,你可以使用explode
php中的函数
$timestamp = "2012-04-02 02:57:54"
$datetime = explode(" ",$timestamp);
$date = $datetime[0];
$time = $datetime[1];
回答by Robert Lattery
$mydatetime = "2012-04-02 02:57:54";
$datetimearray = explode(" ", $mydatetime);
$date = $datetimearray[0];
$time = $datetimearray[1];
$reformatted_date = date('d-m-Y',strtotime($date));
$reformatted_time = date('Gi.s',strtotime($time));
回答by Tushar Nitave
You can try this:
你可以试试这个:
For Date:
日期:
$date = new DateTime($from_date);
$date = $date->format('d-m-Y');
For Time:
时间:
$time = new DateTime($from_date);
$time = $time->format('H:i:s');
回答by zzapper
$timestamp='2014-11-21 16:38:00';
list($date,$time)=explode(' ',$timestamp);
// just time
// 只是时间
preg_match("/ (\d\d:\d\d):\d\d$/",$timestamp,$match);
echo "\n<br>".$match[1];
回答by Ivan Laharnar mink.si
Works for me:
对我有用:
select DATE( FROM_UNIXTIME( columnname ) ) from tablename;
回答by Slava Rozhnev
Optionally you can use database function for date/time formatting. For example in MySQL query use:
您可以选择使用数据库函数进行日期/时间格式化。例如在 MySQL 查询中使用:
SELECT DATE_FORMAT(DATETIMEAPP,'%d-%m-%Y') AS date, DATE_FORMT(DATETIMEAPP,'%H:%i:%s') AS time FROM yourtable
I think that over databases provides solutions for date formatting too
我认为数据库也提供了日期格式的解决方案