PHP中是否有一个内置函数可以将日期转换为字符串?
我已经试过了
date("M jS, Y", strtotime("2014-04-12"));
但我们的客户不想要这种输出。
假设我有一个php变量持有日期
$dateToday = '2014-04-12'
我想要这种格式的输出:"2014年4月4日",也可以是"2014年四月4日"
有没有PHP内置函数可以让我做到这一点?如果没有,请建议我如何做到这一点。
date()函数没有这样的参数,手动转换并不困难。
我假设您在一个变量中有日期、月份和年份。
- 创建一个名称为1-31的数组
- 创建一个月份名称在1到12之间的数组
- 创建一个数组,其中包含1到99之间的年份数字
将这三件事用空格连接起来,就有了输出。互联网上也有图书馆为你做这件事。
您可以简单地分别减去月份、年份和日期,并分别使用开关情况。
我就是这么做的。
$dateToday = '2014-04-12';
$year = substr($dateToday, 0, 4);
$month = substr($dateToday, 5, 2);
$date = substr($dateToday, 8, 2);
echo $dateToday.'<br/>';
switch($year) {
case 2014 : $year_word = 'Two Thousand And Fourteen';
//You can add as many years as you want
}
switch($month) {
case 04 : $month_word = 'April';
//You can add cases from 1 to 12
}
switch($date) {
case 12 : $date_word = 'Twelve';
//You can add cases from 0 to 31
}
$dateToday_new = $date_word.' '.$month_word.' '.$year_word;
echo $dateToday_new;
我希望这对你有所帮助
使用梨包装Numbers_Words:
$dateToday = '2014-04-12';
$date = date('d-F-Y', strtotime($dateToday));
list($day, $month, $year) = explode('-', $date);
$NumbersWords = new Numbers_Words();
$dateWord = implode(' ', array(
$NumbersWords->toWords($day),
$month,
$NumbersWords->toWords($year)
));
echo ucwords($dateWord);