切换案例 - 并非所有案例都有效
目前我正在写日历。根据所选的 motn ($monthnum
),我将缩写的月份名称 ($monthabbr
) 存储在数据库中。为此,我使用 switch-case 结构。它适用于除 8 月 8 日和 9 月 9 日之外的所有月份。由于我几个月都使用相同的代码,我不知道为什么它不起作用。我已经接近重新开始的边缘,但在此之前我最好询问一下您是否看到错误。
switch( $monthnum ) {
case 01:
$monthabbr = 'Jan';
break;
case 02:
$monthabbr = 'Feb';
break;
case 03:
$monthabbr = 'Mär';
break;
case 04:
$monthabbr = 'Apr';
break;
case 05:
$monthabbr = 'Mai';
break;
case 06:
$monthabbr = 'Jun';
break;
case 07:
$monthabbr = 'Jul';
break;
case 08:
$monthabbr = 'Aug';
break;
case 09:
$monthabbr = 'Sep';
break;
case 10:
$monthabbr = 'Okt';
break;
case 11:
$monthabbr = 'Nov';
break;
case 12:
$monthabbr = 'Dez';
break;
}
At the moment I am writing a calendar. According to the chosen motn ($monthnum
), I store the abbreviated month name ($monthabbr
) in a database. For that I use a switch-case construct. It works for all months, except for 08-August and 09-September. Since I used the same code for all months, I don't know why it's not working. I am close to the edge to start all over again, but before that I'll better ask if you see an error.
switch( $monthnum ) {
case 01:
$monthabbr = 'Jan';
break;
case 02:
$monthabbr = 'Feb';
break;
case 03:
$monthabbr = 'Mär';
break;
case 04:
$monthabbr = 'Apr';
break;
case 05:
$monthabbr = 'Mai';
break;
case 06:
$monthabbr = 'Jun';
break;
case 07:
$monthabbr = 'Jul';
break;
case 08:
$monthabbr = 'Aug';
break;
case 09:
$monthabbr = 'Sep';
break;
case 10:
$monthabbr = 'Okt';
break;
case 11:
$monthabbr = 'Nov';
break;
case 12:
$monthabbr = 'Dez';
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
01
、02
、...、09
更改为1
、2
, ...,9
(去掉零)。如果整数文字以
0
开头,则表明它应被解释为八进制数(以 8 为基数的数字)。对于八进制数,数字
8
和9
是非法的。进一步阅读:
(顺便说一句,您可能想要考虑使用数组或从整数到字符串的映射,然后使用诸如
monthAbbrs[$monthnum]
之类的内容查找字符串)Change
01
,02
, ...,09
to just1
,2
, ...,9
(drop the zeros).By starting an integer literal with a
0
indicates that it should be interpreted as an octal number (a number in base 8).For octal numbers the digits
8
and9
are illegal.Further reading:
(Btw, you may want to consider using an array or a map from integer to string, and just look up the string using something like
monthAbbrs[$monthnum]
)您可以通过使用引号来避免此问题
You can avoid this issue by using quotes
我不知道目的是什么,但更好的方法是解析您的日期
,请参阅 此处
,然后按照您喜欢的方式对其进行格式化:
请参阅此处
I dont know what the purpose is but a better way is to parse your date
see here
and then format it in the way you like:
see here