还有一个PHP日期问题要问大家-我有我的网站英文版的当前代码,它运行得很好:
$article = &$articles[$i];
if($currentdate != date("F Y", strtotime($article->getDate()))) {
$currentdate = date("F Y", strtotime($article->getDate()));
$current_content1 .= "<div class='press-articles-heading'>" . $currentdate . " </div>";
}
基本上,这是从数据库中提取记录并比较日期,以便月份标题只显示一次,例如2012年7月,然后是该月份的所有记录,然后是2012年6月等。
如果我在非英语版本的网站上使用相同的代码,我会遇到一个错误:
$article = &$articles[$i];
if($currentdate != date("F Y", strtotime($article->getDate()))) {
$currentdate = strftime("%B %Y", strtotime($article->getDate()));
$current_content1 .= "<div class='press-articles-heading'>" . $currentdate . "</div>";
}
这里发生的情况是,尽管每个数据库记录都是相同的,但我会得到它们的月头。
看不到bug在哪里,有人知道吗?
非常感谢您的帮助。
更新
在发布问题之前,我确实尝试了一些事情,但为了让问题简单,我认为最好不要提。
我确实尝试了以下内容:
$article = &$articles[$i];
if($currentdate != date("F Y", strtotime($article->getDate()))) {
$translated_date = strftime("%B %Y", strtotime($article->getDate()));
$current_content1 .= "<div class='press-articles-heading'>" . $translated_date . "</div>";
}
但这并没有解决问题
试试这个:
$article = &$articles[$i];
$timeString = strtotime($article->getDate());
$numericDate = strftime('%m %Y', $timeString);
if($currentDate != $numericDate) {
$currentDate = $numericDate;
$translatedDate = strftime('%B %Y', $timeString);
$current_content1 .= "<div class='press-articles-heading'>" . $translatedDate . "</div>";
}
这将根据月份和年份的数值进行比较,但会显示本地化的文本值。我使用了额外的变量来缓存一些日期解析结果。
您正在比较date('F Y')
的值和strftime('%B %Y')
的值。这些可能不匹配。