PHP:在mysql中的时间戳值内拆分日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14457250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:18:15  来源:igfitidea点击:

PHP: splitting the date and time within a timestamp value in mysql

phpmysqldatedatetime

提问by Sms

I have a field called "timestamp" in a database table which stores value in this format :- YYYY-MM-DD HH:MM:SS.

我在数据库表中有一个名为“时间戳”的字段,它以这种格式存储值:- YYYY-MM-DD HH:MM:SS。

I would like to split apart and then fetch the date (YYYY-MM-DD) in a variable and also the time (HH:MM:SS) in another variable. Example:

我想拆分然后获取一个变量中的日期(YYYY-MM-DD)以及另一个变量中的时间(HH:MM:SS)。例子:

$timestamp = "2012-10-19 18:19:56";
$get_date = "2012-10-19";
$get_time = "18:19:56";

I would be glad if anyone can help out with this using php.

如果有人可以使用 php 帮助解决这个问题,我会很高兴。

回答by Lix

You could simply split the string by the space character using PHP's explode()function-

您可以使用 PHP 的explode()函数简单地按空格字符拆分字符串-

$timestamp = "2012-10-19 18:19:56";
$splitTimeStamp = explode(" ",$timestamp);
$date = $splitTimeStamp[0];
$time = $splitTimeStamp[1];

Another way of doing this would be this would be to use strtotime()combined with date()-

这样做的另一种方法是strtotime()结合使用date()-

$date = date('Y-m-d',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));

回答by Php Geek

Thats simple, use explode, try this below it wil work

很简单,用explode,试试下面这个就行了

$new_time = explode(" ",$timestamp);
$get_date = $new_time[0];
$get_time = $new_time[1];

回答by Salman A

You could use the explodefunction; plus listfunction could make your code slightly shorter:

您可以使用该explode功能;pluslist函数可以使您的代码稍微短一些:

list($get_date, $get_time) = explode(" ", "2012-10-19 18:19:56");
var_dump($get_date, $get_time);

回答by Miqdad Ali

$timestamp = "2012-10-19 18:19:56";

$splits =  explode(" ",$timestamp);

$get_date = $splits[0];

$get_time = $splits[1];

回答by Muhammad Raheel

instead of exploding you can simply do it like this. This will help you for what ever format you have.

而不是爆炸,你可以简单地这样做。这将帮助您处理您拥有的任何格式。

echo $timestamp = "2012-10-19 18:19:56";
echo '<br>';
echo $data = date('Y-m-d',strtotime($timestamp));
echo '<br>';
echo $time = date('H:i:s',strtotime($timestamp));

Out put

输出

2012-10-19 18:19:56
2012-10-19
18:19:56

回答by jamil

check this PHP fiddle:

检查这个 PHP 小提琴:

http://phpfiddle.org/lite?code=%3C?php\n\n$datetime%20=%20%2228-1-2011%2014:32:55%22;\nlist($date,%20$time)=explode(%27%20%27,%20$datetime);\n\n//%20check%20the%20result\necho%20%22date:%22.%20$date;\necho%20%22%3Cbr%3Etime:%22.%20$time;\n\n//%20further%20more%20you%20can%20easily%20split%20the%20date%20into\n//%20year%20month%20and%20day\nlist($year,%20$month,%20$day)=explode(%27-%27,%20$date);\n\n?%3E\n

http://phpfiddle.org/lite?code=%3C?php\n\n$d​​atetime%20=%20%2228-1-2011%2014:32:55%22;\nlist($date,%20 $time)=explode(%27%20%27,%20$datetime);\n\n//%20check%20the%20result\necho%20%22date:%22.%20$date;\necho%20 %22%3Cbr%3Etime:%22.%20$time;\n\n//%20further%20more%20you%20can%20easy%20split%20the%20date%20into\n//%20year%20month%20and% 20day\nlist($year,%20$month,%20$day)=explode(%27-%27,%20$date);\n\n?%3E\n