在 PHP 中将数字转换为月份名称

我有这个 PHP 代码:

$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", strtotime($monthNum));


echo $monthName;

但它返回的是 December而不是 August

$result["month"]等于8,因此 sprintf函数将添加一个 0使其成为 08

429364 次浏览

您需要使用 strtotimemktime设置字段

echo date("F", strtotime('00-'.$result["month"].'-01'));

如果 mktime只设置了一个月,试试这个:

echo date("F", mktime(0, 0, 0, $result["month"], 1));

strtotime 需要标准的日期格式,并返回一个时间戳。

您似乎传递给 strtotime一个数字来输出日期格式。

您应该使用以 date 元素为参数的 mktime

你的完整代码:

$monthNum = sprintf("%02s", $result["month"]);
$monthName = date("F", mktime(null, null, null, $monthNum));


echo $monthName;

但是,mktime 函数不需要月号的前导零,因此第一行完全没有必要,可以直接将 $result["month"]传递给函数。

然后可以将所有这些内容组合成一行,重复内嵌的日期。

您的重构代码:

echo date("F", mktime(null, null, null, $result["month"], 1));

...

正因为每个人都在使用 strtotime ()和 date ()函数,我将展示 日期时间示例:

$dt = DateTime::createFromFormat('!m', $result['month']);
echo $dt->format('F');

建议的方法是:

现在,任何日期/时间数学都应该使用 DateTime 对象。这需要 PHP 版本 > = 5.2。如 格拉维的回答所示,您可以使用以下方法:

$monthNum  = 3;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); // March

!格式字符用于将所有内容重置为 Unix 时代m格式字符是一个月的数字表示形式,前面带有零。

另一种解决方案:

如果您使用的是较旧的 PHP 版本,而且目前无法升级,那么您可以使用这个解决方案。 date()函数的第二个参数接受一个时间戳,您可以使用 mktime()创建一个时间戳,如下所示:

$monthNum  = 3;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March

如果您想要3个字母的月份名称,如 Mar,将 F改为 M。所有可用格式化选项的列表可以在 PHP 手册文档中找到。

使用 mktime():

<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; // Output: May
?>

参见 PHP 手册: http://php.net/mktime

要对当前语言环境进行转换,可以使用 strftime函数:

setlocale(LC_TIME, 'fr_FR.UTF-8');
$monthName = strftime('%B', mktime(0, 0, 0, $monthNumber));

date不尊重现场 strftime尊重现场。

如果您只想要一个从年初到年底的月份名数组,例如,填充一个下拉选择,我只需要使用以下内容;

for ($i = 0; $i < 12; ++$i) {
$months[$m] = $m = date("F", strtotime("January +$i months"));
}

我认为使用 cal _ info ()是将数字转换为字符串的最简单方法。

$monthNum = sprintf("%02s", $result["month"]); //Returns `08`
$monthName = cal_info(0); //Returns Gregorian (Western) calendar array
$monthName = $monthName[months][$monthNum];


echo $monthName; //Returns "August"

看看 Cal _ info ()的 docs

用途:

$name = jdmonthname(gregoriantojd($monthNumber, 1, 1), CAL_MONTH_GREGORIAN_LONG);

根据需要适应

$m='08';
$months = array (1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec');
echo $months[(int)$m];

这是非常容易的,为什么这么多人提出这么糟糕的建议?@ Bora 是最接近的,但这是最强壮的

/***
* returns the month in words for a given month number
*/
date("F", strtotime(date("Y")."-".$month."-01"));

这就是解决问题的方法

如果你有月份号,你可以首先从中创建一个日期,默认日期是当前年份的第一个年份,然后从创建的日期中提取月份名称:

echo date("F", strtotime(date("Y") ."-". $i ."-01"))

这段代码假设您的月号存储在 $i 中

我目前正在使用下面的解决方案来解决同样的问题:

//set locale,
setlocale(LC_ALL,"US");


//set the date to be converted
$date = '2016-08-07';


//convert date to month name
$month_name =  ucfirst(strftime("%B", strtotime($date)));


echo $month_name;

要了解更多关于 set locale 的信息,请访问 http://php.net/manual/en/function.setlocale.php

要了解更多关于 strftime 的信息,请访问 http://php.net/manual/en/function.strftime.php

Ucfirst()用于将字符串中的第一个字母大写。

$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));

我在 https://css-tricks.com/snippets/php/change-month-number-to-month-name/上找到了这个,它运行得很好。

有许多方法可以从给定的数字中打印出一个月。为您选择一个套件。

函数和参数‘ F’一起使用

代码示例:

$month_num = 10;
echo date("F", mktime(0, 0, 0, $month_num, 10)); //output: October

2. 使用 createFromFormat ()创建 php date 对象

代码示例

$dateObj   = DateTime::createFromFormat('!m', $monthNum);
echo "month name: ".$dateObj->format('F'); // Output: October

3. strtotime ()函数

echo date("F", strtotime('00-'.$monthNum.'-01')); // Output: October

4. mktime ()函数

echo date("F", mktime(null, null, null, $monthNum)); // Output: October

5. 使用 jdmonthname ()

$jd=gregoriantojd($monthNum,10,2019);
echo jdmonthname($jd,0); // Output: Oct

这适用于日期时间转换的所有需要

 <?php
$newDate = new DateTime('2019-03-27 03:41:41');
echo $newDate->format('M d, Y, h:i:s a');
?>

你只需要一句话就可以做到:

DateTime::createFromFormat('!m', $salary->month)->format('F'); //April

我就是这么做的

// sets Asia/Calcutta time zone
date_default_timezone_set("Asia/Calcutta");


//fetches current date and time
$date = date("Y-m-d H:i:s");


$dateArray = date_parse_from_format('Y/m/d', $date);
$month = DateTime::createFromFormat('!m', $dateArray['month'])->format('F');
$dateString = $dateArray['day'] . " " . $month  . " " . $dateArray['year'];


echo $dateString;

返回 30 June 2019

这里有一个简单的技巧,您可以根据需要使用 strtotime ()函数,将数字转换为月份名称。

1.如果你想在 Jan,Feb 和 Mar 中得到一个结果,那么试试下面的一个,在日期中用‘ M’作为参数。

$month=5;
$nmonth = date('M',strtotime("01-".$month."-".date("Y")));
echo $nmonth;

产量: 5月

/2.您可以尝试使用‘ F’而不是‘ M’来获得作为输出的完整月份名称,如一月、二月、三月等。

$month=1;
$nmonth = date('M',strtotime("01-".$month."-".date("Y")));
echo $nmonth;

产出: 1月

$days = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$month = ( date('m') < 10 ) ? date('m')[1] : date('m');

提取月份。

这方面的 LC _ TIME

$date = new DateTime('2022-04-05');
$mes = strftime('%B', $date->getTimestamp());

我的方法

$month = date('m');
$months_of_year = array(
array('month' => '01', 'translation' => 'janeiro'),
array('month' => '02', 'translation' => 'fevereiro'),
array('month' => '03', 'translation' => 'março'),
array('month' => '04', 'translation' => 'abril'),
array('month' => '05', 'translation' => 'maio'),
array('month' => '06', 'translation' => 'junho'),
array('month' => '07', 'translation' => 'julho'),
array('month' => '08', 'translation' => 'agosto'),
array('month' => '09', 'translation' => 'setembro'),
array('month' => '10', 'translation' => 'outubro'),
array('month' => '11', 'translation' => 'novembro'),
array('month' => '12', 'translation' => 'dezembro'),
);
$current_month = '';
foreach ($months_of_year as $key => $value)
{
if ($value['month'] == $month){
$current_month = $value['translation'];
break;
}
}
echo("mes:" . $current_month);