PHP转换月份

发布于 2024-10-09 12:24:52 字数 198 浏览 0 评论 0原文

无论如何,是否可以将日期月份数字转换为字符串。

例如,1 转换为一月,2 =>二月等

我在下面尝试过

<?php echo date('F', strtotime($member['dob_month'])); ?>

但没有成功

is there anyway to convert date month numeric to string.

e.g. for 1 convert to January, 2 => February, etc

i tried below

<?php echo date('F', strtotime($member['dob_month'])); ?>

didnt work out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉城已无爱 2024-10-16 12:24:52
<?php echo date("F",mktime(0,0,0,$member['dob_month'],1,0); ?>
<?php echo date("F",mktime(0,0,0,$member['dob_month'],1,0); ?>
澉约 2024-10-16 12:24:52
$months = array(1 => 'January', 2 => 'February', ...);
echo $months[$member['dob_month']];

给定 $member['dob_month'] 的值是一个从 1 开始的整数。

$months = array(1 => 'January', 2 => 'February', ...);
echo $months[$member['dob_month']];

Given the value of $member['dob_month'] is an 1-based integer.

不弃不离 2024-10-16 12:24:52

您可以尝试使用 PHP 的 DateTime 类。

$date = DateTime::createFromFormat('n', $member['dob_month']);
echo $date->format('F');

注意:在 createFromFormat 中,使用 'm ' 如果月份有前导零,则为 'n' 如果没有。

You can try to use PHP's DateTime class.

$date = DateTime::createFromFormat('n', $member['dob_month']);
echo $date->format('F');

Note: In createFromFormat, use 'm' if the month has leading zeros, 'n' if it doesn't.

(り薆情海 2024-10-16 12:24:52

您的问题是 date() 需要为其第二个参数提供时间戳,并且 strtotime($member['dob_month']) 如果 < code>$member['dob_month'] 是 1 到 12 之间的数字。

您可以使用类似以下内容的内容:

date("F", mktime(0, 0, 0, $member['dob_month'], 1, 2010));

Your problem is that date() needs a timestamp for it´s second parameter and strtotime($member['dob_month']) does not result in a meaningfull timestamp if $member['dob_month'] is a number from 1 to 12.

You can use something like:

date("F", mktime(0, 0, 0, $member['dob_month'], 1, 2010));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文