php 将日期时间拆分为日期和时间值

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

Splitting Datetime into a date and a time value

phpmysql

提问by jose quervo

Can someone give me a quick and dirty way to split a datetime (28-1-2011 14:32:55) into just the date (28-1-2011) and the time ( 14:32 ) or even better (2:32 PM) using PHP. Using a mySQL database as well.

有人能给我一个快速而肮脏的方法来将日期时间 (28-1-2011 14:32:55) 拆分为日期 (28-1-2011) 和时间 (14:32) 甚至更好 (2:下午 32 点)使用 PHP。也使用 mySQL 数据库。

Cheers

干杯

回答by Dan Soap

If you're using PHP > 5.2:

如果您使用的是 PHP > 5.2:

$myvalue = '28-1-2011 14:32:55';

$datetime = new DateTime($myvalue);

$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');

Prior to PHP 5.2 mhitzagave a good answer.

在 PHP 5.2 之前,mhitza给出了很好的答案。

回答by bensiu

if your source of data is MySQL:

如果您的数据源是 MySQL:

SELECT DATE( date_field ) AS date_part, TIME( date_field ) AS time_part ....

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date

Edit : to answer the question from comments (example):

编辑:回答评论中的问题(示例):

WHERE DATE( date_field ) > '2017-01-01'

回答by mhitza

In php you can use the dateand strtotimefunctions for easy extraction.

在 php 中,您可以使用datestrtotime函数来轻松提取。

$datetime = "28-1-2011 14:32:55";
$date = date('Y-m-d', strtotime($datetime));
$time = date('H:i:s', strtotime($datetime));

回答by jamil

One simple instruction will do the trick

一个简单的指令就可以解决问题

explode will transform datetime to an array

爆炸将日期时间转换为数组

and list will sort the datetime array into its needed values

和列表将日期时间数组排序为其所需的值

$datetime = "28-1-2011 14:32:55";
list($date, $time)=explode(' ', $datetime);

// check the result
echo "date:". $date;
echo "<br>time:". $time;

// further more you can easily split the date into 
// year month and day 
list($year, $month, $day)=explode('-', $date); 

回答by Anandhu Raj

We can easily split DateTime(28-1-2011 14:32:55) into date and time in MySQL.

我们可以轻松地将 DateTime(28-1-2011 14:32:55) 拆分为 MySQL 中的日期和时间。

select SUBSTRING_INDEX("28-1-2011 14:32:55", " ",1) into @dateOnly;

select @dateOnly;

The output will be- 28-1-2011(Here we split the date from the DateTime)

输出将是 - 28-1-2011(这里我们将日期与日期时间分开)

select SUBSTRING_INDEX("28-1-2011 14:32:55", " ",-1) into @timeOnly;

select @timeOnly;

The output will be- 14:32:55(Here we split the time from the DateTime)

输出将是 - 14:32:55(这里我们将时间与 DateTime 分开)

We can covert the time to am and pm format also

我们也可以将时间转换为 am 和 pm 格式

select SUBSTRING_INDEX("28-1-2011 14:32:55", " ",-1) into @timeOnly;

SELECT TIME_FORMAT(@timeOnly, "%h %i %s %p")into @time;

select @time;

The time format will become 02 32 55 PM

时间格式将变为02 32 55 PM

回答by Steve

If you looking for a really quick and dirty solution.

如果您正在寻找一个非常快速和肮脏的解决方案。

$datetime = "28-1-2011 14:32:55";
$date_arr= explode(" ", $datetime);
$date= $date_arr[0];
$time= $date_arr[1];

回答by Mohammad

if you want to parse in the date from your Mysql and you want to remove time then you can use this function

如果你想从你的 Mysql 中解析日期并且你想删除时间,那么你可以使用这个函数

$date1=date("Y-m-d",strtotime('$your database field'))