编程爱好者之家
//添加文章访问量 var add_visit_url = "/Home/Ajax/addArticleVisit"; $(function(){ var id = 20180302103653; var hit_day = 20180302; $.ajax({ type:"post", url:add_visit_url, data:{id:id,hit_day:hit_day}, success:function(data) { } }) })
php代码如下
public function addVisitCount(){ $id = I('id'); $hits_day = I("hit_day"); $tmpdate = date('Ymd'); $lastweek = LastWeekday(); //上周日期 $nextweek = NextWeekday(); //下周日期 $preMonth = date("Ymd", strtotime("-1 month")); $nextMonth = date("Ymd", strtotime("+1 month")); if ($hits_day == $tmpdate){ $strQuery = "day_hits=day_hits+1 ,"; }else{ $strQuery = "day_hits=1 ,"; } if (($hits_day >= $lastweek) && ($hits_day <= $nextweek)){ $strQuery .= "week_hits=week_hits+1, "; }else{ $strQuery .= " week_hits=1 ,"; } if (($hits_day >= $preMonth) && ($hits_day <= $nextMonth)){ $strQuery .= "month_hits=month_hits+1 "; }else{ $strQuery .= "month_hits=1 "; } $sql = " UPDATE blog_article SET hits=hits+1,hit_day='$tmpdate',"; $sql .= $strQuery; $sql .= " WHERE `id` =$id"; $this->_article -> execute($sql); }
function LastWeekday(){ $tmpyear = date('Y'); $tmpmonth = date('m'); $tmpday = date('d'); $temp = date('w'); // the day of the week if ($temp == 0) $temp = 7; // 0 is sunday // 每个星期以星期1为开始,星期天为结束,所以是为了获得所在这个星期的开始和结束 if ($tmpday < $temp){ // 那就是说上个开始肯定是上个月的,比如今天是1号却是星期天,那上个开始肯定是上个月 $lastmonth = $tmpmonth - 1; if ($lastmonth < 1){ // 也就是说上个月是12月咯~ $lastmonth = 12; $lastyear = $tmpyear - 1; //这里就不再处理了~ }else{ $lastmonth = sprintf("%02d", $lastmonth); // $lastyear = $tmpyear; } $lastmonthdays = monthdays($lastmonth, $lastyear); $lastday = $lastmonthdays + $tmpday - $temp + 1; // 比如今天是8月2号,星期5,然后31+2-5 + 1 = 29 也就是说上个月29是星期1 // 再比如2002.9.1是星期天,那么31+1-7+1 = 26~~:) }else{ // 那上个开始肯定就是这个月的咯,如果都是7的话就是第一天咯~比如今天是1号是星期1,那今天就是开始 $lastmonth = sprintf("%02d", $tmpmonth); // $lastyear = $tmpyear; $lastday = $tmpday - $temp + 1; //比如9.2是星期1,那么2-1+1 = 2 就是今天咯~ } $lastday = sprintf("%02d", $lastday); $lastweek = "$lastyear$lastmonth$lastday"; return $lastweek; }
function NextWeekday(){ $tmpyear = date('Y'); $tmpmonth = date('m'); $tmpday = date('d'); $temp = date('w'); // the day of the week if ($temp == 0) $temp = 7; // 0 is sunday // 那今天离这个星期的结束还有天数 $temp2 = 7 - $temp; $nextday = $tmpday + $temp2; $monthdays = monthdays($tmpmonth, $tmpyear); //这个月的天数 if ($nextday > $monthdays){ // 也就是说在下个月 $nextmonth = $tmpmonth + 1; if ($nextmonth > 12){ $nextmonth = sprintf("%02d", 1); $nextyear = $tmpyear + 1; }else{ $nextmonth = sprintf("%02d", $nextmonth); $nextyear = $tmpyear; } $nextday = $nextday - $monthdays; // 如8.30是星期5,那32-31 = 1 }else{ // 也就是说在这个月 $nextyear = $tmpyear; $nextmonth = sprintf("%02d", $tmpmonth); $nextday = $nextday; // 如8.20是星期2,那就是25 } $nextday = sprintf("%02d", $nextday); $nextweek = "$nextyear$nextmonth$nextday"; return $nextweek; }
function monthdays($month, $year){ if ($month == 1 or $month == 3 or $month == 5 or $month == 7 or $month == 8 or $month == 10 or $month == 12){ $days = 31; }elseif ($month == 4 or $month == 6 or $month == 9 or $month == 11){ $days = 30; }elseif ($month == 2){ if (($year-1980) % 4 == 0){ // ^-^ $days = 29; }else{ $days = 28; } } return $days; }