如何检查给定字符串是否为有效月份? [英] How do I check that the given string is a valid month or not?
本文介绍了如何检查给定字符串是否为有效月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作检查月份是否有效的功能。例如。
I'm making a function to check the month is valid or not. For eg.
checkMonth("Feb") => true
checkMonth("Mar") => true
checkMonth(02) => true
checkMonth(2) => true
但是
But
checkMonth("X") => false
checkMonth("XYZ") => false
数字形式2或02没有问题。但如果我传递的参数如X或XYZ 它不起作用并且返回真实。
我正在尝试
There is no issue in the numeric form 2 or 02. But if I'm passing argument like "X" or "XYZ" its not working and returns true.
Im trying
echo date('n', strtotime("XYZ"));
返回true,因为date('n',strtotime(XYZ))的值是3,这是一个有效月份。
我也试过
which is returning true because the value of date('n', strtotime("XYZ")) is 3 which is a valid month.
I also tried
$mon=date_format(date_create("XYZ"),"n");
但它具有相同的效果。
But it has the same affect.
推荐答案
mon = date_format(date_create( XYZ), n);
mon=date_format(date_create("XYZ"),"n");
但它具有相同的效果。
But it has the same affect.
尝试这个想法:将所有月份名称放入一个数组,然后使用in_array函数来检查:
http:// php。 net / manual / en / function.in-array.php [ ^ ]
+++++ [第2轮] +++++
ok,试试 strtotime() [ ^ ]
eg
Try this idea: put all the month names in an array, then use in_array function to check:
http://php.net/manual/en/function.in-array.php[^]
+++++[Round 2]+++++
ok, try strtotime()[^]
e.g.
if (strtotime('Jun'))
echo 1;
else
echo 0;
您可以简单地在switch语句中列出所有可能的好答案,其中包含一个直通和其他任何失败。
示例:
You could simply list all of your possible good answers in a switch statement with a fall-through and anything else fails.
Example:
switch(
这篇关于如何检查给定字符串是否为有效月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文