Home > php教程 > php手册 > body text

php判断:在指定日期之后触发事件(完美使用新@功能)

WBOY
Release: 2016-06-06 20:08:38
Original
2000 people have browsed it

首先给个例子,我今天找到一个让回复中加@功能的新方法,利用add_filter而非js,这个方法好处多多,避免了js冲突(如分页功能下)@失效的BUG. add_filter('comment_text','comment_add_at_parent');function comment_add_at_parent($comment_text){$comment_ID = g

首先给个例子,我今天找到一个让回复中加@功能的新方法,利用add_filter而非js,这个方法好处多多,避免了js冲突(如分页功能下)@失效的BUG.

add_filter('comment_text','comment_add_at_parent');
function comment_add_at_parent($comment_text){
$comment_ID = get_comment_ID();
$comment = get_comment($comment_ID);
if ($comment->comment_parent ) {
$parent_comment = get_comment($comment->comment_parent);
$comment_text = '<a href="#comment-'%20.%20%24comment->comment_parent%20.%20'">@'.$parent_comment->comment_author.'</a> ' . $comment_text;
}
return $comment_text;
}
Copy after login

但这个方法有个小弊端,就是它会给所有子评论加上@,而以前用js时访客在评论内容中输入的@还存在于评论内容中, 这就导致旧评论中会出现双@.
解决这个问题的办法就是让指定日期之前的评论不受此函数影响,也就是我们今天要讨论的话题:利用php判断来使事件在指定日期之后触发.
比如今天是2013年3月17日,我今天给主题加上了上述函数,所以我希望这个函数不对2013年3月17日之前的评论起作用,所以我们可以将其改成如下代码:

add_filter('comment_text','comment_add_at_parent');
function comment_add_at_parent($comment_text){
$comment_ID = get_comment_ID();
$comment = get_comment($comment_ID);
$date = mysql2date( 'Ymd',$comment->comment_date) ;//通过mysql2date函数将评论时间日期化,即转为无时间的日期格式,这里的Ymd表示日期格式为"年月日",中间没有分隔符,这很重要,下一行会提到
if( $date > 20130317) {//判断: 如果评论日期大于指定日期,也就是在指定日期之后,则输出下面的函数 (这里就是为什么要用Ymd的格式,因为没有分隔符,可以将其当作纯数字来运算),20130317可以改成你需要的指定日期
if ($comment->comment_parent ) {
$parent_comment = get_comment($comment->comment_parent);
$comment_text = '<a rel="nofollow" href="#comment-'%20.%20%24comment->comment_parent%20.%20'">@'.$parent_comment->comment_author.'</a> ' . $comment_text;
}
}
return $comment_text;
}
Copy after login

最后说一句,有东西折腾真好啊,好久没有文荒的感觉了,好开森.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!