在PHP中如何将Y-m-d转换为Y-m-d ?(复制)

时间:2021-10-04 21:30:11

This question already has an answer here:

这个问题已经有了答案:

How to convert Y-m-d H:i:s to Y-m-d in PHP?

在PHP中如何将Y-m-d转换为Y-m-d ?

I have e.g.

我如。

$date = "2011-08-10 20:40:12";

and would like to convert it to just

把它转换成

$output = "2011-08-10";

Thanks in advance.

提前谢谢。

10 个解决方案

#1


14  

quick/dirty:

快速/肮脏的:

$output = substr('2011-08-10 20:40:12', 0, 10);

slightly more robust:

更健壮的:

$output = date('Y-m-d', strtotime('2011-08-10 20:40:12'));

fairly reliable:

相当可靠:

$output = DateTime::createFromFormat('Y-m-d h:i:s', '2011-08-10-20:40:12')->format('Y-m-d');

#2


5  

Easily done with strtotime(), and capable of changing to any other date format you may need as well.

使用strtotime()很容易,并且可以更改为您可能需要的任何其他日期格式。

$old_date = "2011-08-10 20:40:12";
$new_date = date("Y-m-d", strtotime($old_date));
echo $new_date;

// Prints 2011-08-10

#3


3  

Well, a simple way to do it would be the following (if you've already gotten the date in a string):

一个简单的方法是这样的(如果你已经用字符串表示了日期):

$output = substr($date, 0, 10);

This would mean that you grab the first 10 characters in "2011-08-10 20:40:12", leaving you with "2011-08-10"!

这就意味着你要抓住“2011-08-10 20:40:12”中的前10个字符,剩下的就是“2011-08-10”!

#4


3  

You can use the built in php date format function:

您可以使用内置的php日期格式函数:

http://php.net/manual/en/datetime.format.php

http://php.net/manual/en/datetime.format.php

to do this:

要做到这一点:

$date = date_format($date, 'Y-m-d');

#5


2  

Another solution, thought not as good is to simply do a substring:

另一个不太好的解决方案是做一个子字符串:

$newDate = substr($date, 0, 10)

#6


1  

$date = date("Y-m-d", strtotime($date)); 

Just use the date format for date only and convert your string to a timestamp.

只对日期使用日期格式,并将字符串转换为时间戳。

#7


1  

I prefer strftime to date since it's format input matches those in C and other languages.

我更喜欢strftime,因为它的格式输入与C和其他语言的匹配。

$output = strftime( '%Y-%m-%d', strtotime( $date) );

#8


0  

A tweak to the method using explode so you can turn it into a one liner:

一种使用“爆炸”的方法的微调,这样你就可以把它变成一条直线:

list($date) = explode(' ', $date);

列表($date) =爆炸(',$date);

the list function is very handy for dates.

list函数对于日期非常方便。

Another example using list:

另一个例子使用列表:

list($year, $month, $day) = explode(' ', $date);

列表($year, $month, $day) =爆炸式增长(',$date);

Also, you are best to use ' instead of " in strings, unless your string includes variables.

此外,最好在字符串中使用'而不是',除非您的字符串包含变量。

e.g.

如。

  • 'yada'
  • “游泳”
  • 'bing'
  • “必应”
  • "Hello {$bob}"
  • “你好,{ $鲍勃}”

I'm fed up seeing even the PHP docs use "string" in examples where they should use 'string' :o)

我受够了PHP文档在示例中使用“string”,它们应该使用“string”:o)

#9


0  

$old_date = "2011-08-10 20:40:12";
$new_date = date("Y-m-d", strtotime($old_date));
echo $new_date;

// Prints 2011-08-10

This should be very useful.

这应该非常有用。

#10


-1  

Try:

试一试:

$date = explode(" ",$date);
$output = $date[0];

If they are separated by space, you can use explode to separate them.

如果它们被空间分开,你可以用爆炸把它们分开。

#1


14  

quick/dirty:

快速/肮脏的:

$output = substr('2011-08-10 20:40:12', 0, 10);

slightly more robust:

更健壮的:

$output = date('Y-m-d', strtotime('2011-08-10 20:40:12'));

fairly reliable:

相当可靠:

$output = DateTime::createFromFormat('Y-m-d h:i:s', '2011-08-10-20:40:12')->format('Y-m-d');

#2


5  

Easily done with strtotime(), and capable of changing to any other date format you may need as well.

使用strtotime()很容易,并且可以更改为您可能需要的任何其他日期格式。

$old_date = "2011-08-10 20:40:12";
$new_date = date("Y-m-d", strtotime($old_date));
echo $new_date;

// Prints 2011-08-10

#3


3  

Well, a simple way to do it would be the following (if you've already gotten the date in a string):

一个简单的方法是这样的(如果你已经用字符串表示了日期):

$output = substr($date, 0, 10);

This would mean that you grab the first 10 characters in "2011-08-10 20:40:12", leaving you with "2011-08-10"!

这就意味着你要抓住“2011-08-10 20:40:12”中的前10个字符,剩下的就是“2011-08-10”!

#4


3  

You can use the built in php date format function:

您可以使用内置的php日期格式函数:

http://php.net/manual/en/datetime.format.php

http://php.net/manual/en/datetime.format.php

to do this:

要做到这一点:

$date = date_format($date, 'Y-m-d');

#5


2  

Another solution, thought not as good is to simply do a substring:

另一个不太好的解决方案是做一个子字符串:

$newDate = substr($date, 0, 10)

#6


1  

$date = date("Y-m-d", strtotime($date)); 

Just use the date format for date only and convert your string to a timestamp.

只对日期使用日期格式,并将字符串转换为时间戳。

#7


1  

I prefer strftime to date since it's format input matches those in C and other languages.

我更喜欢strftime,因为它的格式输入与C和其他语言的匹配。

$output = strftime( '%Y-%m-%d', strtotime( $date) );

#8


0  

A tweak to the method using explode so you can turn it into a one liner:

一种使用“爆炸”的方法的微调,这样你就可以把它变成一条直线:

list($date) = explode(' ', $date);

列表($date) =爆炸(',$date);

the list function is very handy for dates.

list函数对于日期非常方便。

Another example using list:

另一个例子使用列表:

list($year, $month, $day) = explode(' ', $date);

列表($year, $month, $day) =爆炸式增长(',$date);

Also, you are best to use ' instead of " in strings, unless your string includes variables.

此外,最好在字符串中使用'而不是',除非您的字符串包含变量。

e.g.

如。

  • 'yada'
  • “游泳”
  • 'bing'
  • “必应”
  • "Hello {$bob}"
  • “你好,{ $鲍勃}”

I'm fed up seeing even the PHP docs use "string" in examples where they should use 'string' :o)

我受够了PHP文档在示例中使用“string”,它们应该使用“string”:o)

#9


0  

$old_date = "2011-08-10 20:40:12";
$new_date = date("Y-m-d", strtotime($old_date));
echo $new_date;

// Prints 2011-08-10

This should be very useful.

这应该非常有用。

#10


-1  

Try:

试一试:

$date = explode(" ",$date);
$output = $date[0];

If they are separated by space, you can use explode to separate them.

如果它们被空间分开,你可以用爆炸把它们分开。