PHP 在前端显示时更改字段值

PHP change Field Value when displaying on front end

字段名称 'day' 有 5 个值,它们是: 'mon' 'tue' 'wed' 'thur' 或 'fri'.

当我在前端回显这些时:

                <div class="listing-footer-left">

                    <?php $day = get_post_meta( get_the_ID(),'day', true );

                    echo $day; ?>

                </div>

我想将值 'mon' 显示为 'Mondays',将值 'tue' 显示为 'tuesdays' 等等

请告诉我这是怎么可能的。

非常感谢, 马特

您可以像这样使用 datestrtotime

https://3v4l.org/RlHkr

<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l', strtotime($day));
echo $day;
?>

编辑:虽然这只适用于单数,但我认为没有内置的复数语言环境感知解决方案 - 如果您只需要它用于英语,您可以制作一个数组:

https://3v4l.org/L8Jr4

<?php

$arr = ['mon' => 'Mondays', 'tue' => 'Tuesdays']; // ...
$day = 'mon';
echo $arr[$day];

或者使用上面的方法并附加一个 s:

https://3v4l.org/2gkvI

<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l\s', strtotime($day));
echo $day;
?>

参考文献:

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.strtotime.php