PHP将短月份名称转换为月份编号 [英] PHP convert short month-name to month-number

查看:81
本文介绍了PHP将短月份名称转换为月份编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将月份从名称转换为数字

Possible Duplicate:
convert month from name to number

我对所有对象都有一个简单的查询(但很有趣).

I have a simple (yet interesting) query for all.

简而言之,我正在获取月份名称,例如Jan,Feb等.现在,我需要将其转换为month-number(即月份的数字表示形式),以前导零开头

I am getting month name, in short, like Jan, Feb, etc. Now I need to convert it to month-number ( i.e., Numeric representation of a month), with leading zeros

例如:从"Jan"到"01",从"Dec"到"12",等等

Example: "Jan" to "01", "Dec" to "12", etc

任何人都知道,如何实现此目标,不使用数组

Anyone know, how to achieve this, without using array

谢谢

推荐答案

$month = 'Feb';
echo date('m', strtotime($month));

strtotime将"Feb"转换为 2011年2月6日15:15:00 的时间戳(在撰写本文时),从给定的字符串中获取尽可能多的内容并填充空白从当前时间开始. date('m') 然后格式化此时间戳,仅输出月份号.

strtotime converts "Feb" to the timestamp for February 6th 2011 15:15:00 (at the time of writing), taking what it can from the given string and filling in the blanks from the current time. date('m') then formats this timestamp, outputting only the month number.

由于这实际上可能会在31日引起问题,因此您应该填写以下空白以确保:

Since this may actually cause problems on, say, the 31st, you should fill in these blanks to be sure:

echo date('m', strtotime("$month 1 2011"));

这篇关于PHP将短月份名称转换为月份编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆