php - PHP在前端显示时更改字段值
问题描述
字段名称“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”等
请你让我知道这是怎么可能的。
非常感谢,马特
解决方案
你可以像这样使用date
和strtotime
:
<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l', strtotime($day));
echo $day;
?>
编辑:这仅适用于单数,但我认为复数没有内置的区域设置感知解决方案 - 如果您只需要它为英语工作,您可以制作一个数组:
<?php
$arr = ['mon' => 'Mondays', 'tue' => 'Tuesdays']; // ...
$day = 'mon';
echo $arr[$day];
或使用上面的方法,只需附加一个s
:
<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l\s', strtotime($day));
echo $day;
?>
参考:
推荐阅读
- c# - 使用 Newtonsoft.Json 反序列化未定义的值
- python - 如何在带有 python 3.7 的 Windows 10 中使用 conda 安装 gdal?安装geoviews后,我再也没有设法使用gdal
- javascript - my animated toggle navigationbar overlaps?
- azure - Azure 数据工厂数据流 - 动态接收器参数
- google-api - Google Cloud API 密钥权限
- elasticsearch - 带有“匹配”子句数组的“必须”子句的真正含义是什么?
- c# - 在 datagridview 中添加新行会挂起应用程序 C# 表单
- python - 如何创建显示均值、中值和模式的海底小提琴图?
- python - 使用 Python 进行 Shell 交互并提取响应以进行进一步的逻辑
- laravel - 基于 json 数据的 Laravel 自定义认证