从字符串中提取年/月/日

时间:2022-09-13 11:06:36

How can I extract the date in the following string:

如何在以下字符串中提取日期:

"Hästebäck 1960-03-13"

I want to extract 1960-03-13.

我想提取1960-03-13。

3 个解决方案

#1


0  

you can try date_parse function as well

你也可以试试date_parse函数

$string = "Hästebäck 1960-03-13";
$result = date_parse($string);
print_r($result);

it will return you an array like this

它会返回一个像这样的数组

Array ( [year] => 1960 [month] => 3 [day] => 13 [hour] => [minute] => [second] => [fraction] ..........

you can use date like this

你可以像这样使用日期

echo $result['year'].'-'.$result['month'].'-'.$result['day'];

#2


1  

Try PHP explode.

试试PHP爆炸。

<?php

$string = "Hästebäck 1960-03-13";
$array = explode(" ",$string);
echo $array[1];

DEMO

DEMO

#3


1  

([\d-]+)

This should do it.See demo.

这应该这样做。参见演示。

http://regex101.com/r/jT3pG3/34

http://regex101.com/r/jT3pG3/34

Edit:to get year month separately try

编辑:分别获得年月试试

(\d+?)(?=-|$)

See demo

见演示

http://regex101.com/r/jT3pG3/37

http://regex101.com/r/jT3pG3/37

#1


0  

you can try date_parse function as well

你也可以试试date_parse函数

$string = "Hästebäck 1960-03-13";
$result = date_parse($string);
print_r($result);

it will return you an array like this

它会返回一个像这样的数组

Array ( [year] => 1960 [month] => 3 [day] => 13 [hour] => [minute] => [second] => [fraction] ..........

you can use date like this

你可以像这样使用日期

echo $result['year'].'-'.$result['month'].'-'.$result['day'];

#2


1  

Try PHP explode.

试试PHP爆炸。

<?php

$string = "Hästebäck 1960-03-13";
$array = explode(" ",$string);
echo $array[1];

DEMO

DEMO

#3


1  

([\d-]+)

This should do it.See demo.

这应该这样做。参见演示。

http://regex101.com/r/jT3pG3/34

http://regex101.com/r/jT3pG3/34

Edit:to get year month separately try

编辑:分别获得年月试试

(\d+?)(?=-|$)

See demo

见演示

http://regex101.com/r/jT3pG3/37

http://regex101.com/r/jT3pG3/37