PHP:在mysql中分割时间戳值内的日期和时间 [英] PHP: splitting the date and time within a timestamp value in mysql

查看:521
本文介绍了PHP:在mysql中分割时间戳值内的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

如果有人可以使用php帮助,我会很高兴。

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

推荐答案

您可以使用PHP的 explode()功能 -

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

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

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

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

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

这篇关于PHP:在mysql中分割时间戳值内的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆