[英]Convert month number to month short name
I have a variable with the following value 我有一个具有以下值的变量
$month = 201002;
the first 4 numbers represent the year, and the last 2 numbers represent the month. 前4个数字代表年份,最后2个数字代表月份。 I need to get the last 2 numbers in the month string name eg.
我需要在月份字符串名称中获取最后2个数字,例如。 Feb
二月
My code looks like this 我的代码看起来像这样
<?php echo date('M',substr($month,4,6)); ?>
I can I go about to obtain the month name 我可以去获取月份名称
追加“01”并且strtotime将能够解析字符串:
echo date('M', strtotime($month . '01'));
You can use the DateTime Class to get a Date data structure using date string and format. 您可以使用DateTime类来使用日期字符串和格式来获取Date数据结构。 Then get a date string with any format like this:
然后获取任何格式的日期字符串,如下所示:
$month = 201002;
$date = DateTime::createFromFormat('Yd', $month);
$monthName = $date->format('M'); // will get Month name
This may help you.. 这可能对你有帮助..
$month = substr($month, -2, 2);
echo date('M', strtotime(date('Y-'. $month .'-d')));
$mydate = "201002";
date('M', mktime(0, 0, 0, substr($mydate, 4, 2), 1, 2000));
Being a programmer, and even knowing nothing of PHP data magic, I'd made it 作为一名程序员,甚至不了解PHP数据魔法,我做到了
$month = intval(substr($input_date,4,2));
$mons = explode(" ","Zer Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
echo $mons[$month];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.