在php中获取一个单词的前三个字母

时间:2013-01-08 06:32:10

标签: php

我想在php中获得一个单词的前三个字母。

例如:

我的2012年9月5日在我的数据库(mysql)

并且在我的HTML中我只想看到日期的前三个字母,就像在9月份一样,我希望它只在我的html中为“SEP”。

如果我想获得5和2012年该怎么办?

感谢您的回答,但如何获得5和2012?我的意思是白天和一年

3 个答案:

答案 0 :(得分:2)

查看strtotimedate

类似的东西:

$dateStr = "september 5, 2012";
$month = date("M", strtotime($dateStr)); //Sep
$day = date("d", strtotime($dateStr)); //05
$year = date("Y", strtotime($dateStr)); //2012

当然还是:

$dateStr = "september 5, 2012";
$completeDateVariable = date("M jS Y", strtotime($dateStr)); //Sep 5th 2012

答案 1 :(得分:2)

使用substr函数

$date = "September 5, 2012";
$final_date = substr($date, 0, 3);
echo $final_date;

从日期使用中获取日期和年份

//day
$day = date("d", strtotime($date));
//year
$year = date("Y", strtotime($date));

现在您可以使用这3个变量。

答案 2 :(得分:1)

$str='abcdef';

echo substr($str,0,3);