1

我有以下代码,用于获取某个日期的月份名称,然后用于获取希伯来语月份的名称。

$thismonthname = date("F", mktime(0, 0, 0, $thismonthnumber, 10)); 

date_default_timezone_set('Asia/Tel_Aviv');     // Set timezone to Israel   

$locale = 'he_IL.utf8'; setlocale(LC_ALL, $locale); // Set Locale to Hebrew

$thismonthnameheb = strftime('%B', strtotime($thismonthname));

它工作得很好,除了二月。

当我打印出 $thismonthname 时,它​​会显示“二月”,但是当我打印出 $thismonthnameheb 时,它会在希伯来语中显示 מרץ(三月)。

疯了,我想不通。

4

3 回答 3

1

这与希伯来语无关,因为至少在我使用的特定 PHP 版本(5.3.10)上,

echo strftime('%d %B', strtotime('February'));

01 March

正如评论中所建议的,这种可以说是出乎意料的行为基本上是由于 PHP 假设给定月份的第 30 天是第 30 天,除非用户实际指定了不同的值。因此,对于 2 月,我们溢出到 3 月 1 日。

看看这个参考可能会很有用。

于 2014-05-29T16:55:56.717 回答
1

您在字符串和字符串之间进行了太多转换。
无需转换time -> string -> time,只需保留时间值并将结果基于此:

$thismonthtime = mktime(0, 0, 0, $thismonthnumber, 10);
$thismonthname = date("F", $thismonthtime); 

date_default_timezone_set('Asia/Tel_Aviv');     // Set timezone to Israel   
$locale = 'he_IL.utf8'; setlocale(LC_ALL, $locale); // Set Locale to Hebrew

$thismonthnameheb = strftime('%B', $thismonthtime);
于 2014-05-29T16:41:15.533 回答
0

注意顺序:

php > $time = mktime(0,0,0,2, 10);
php > echo date('r', $time);
Mon, 10 Feb 2014 00:00:00 -0600  // everything ok here, I'm in UTC-6
php > date_default_timezone_set('Asia/Tel_Aviv');
php > echo date('r', $time);
Mon, 10 Feb 2014 08:00:00 +0200  // note how it's now 8 hours "later"
php > $month =  date('F', $time);
php > echo $month;
February
php > $newtime = strtotime($month);
php > echo date('r', $newtime);
Sat, 01 Mar 2014 00:00:00 +0200  // hey! it's march again!

您将日期剥离为一个简单的月份名称,然后期望 PHP 能够神奇地猜出您期望做什么,这搞砸了。如果你只喂它一个月,它可以自由选择它想要的年/日/时间值。

于 2014-05-29T16:48:47.153 回答