php计算会员卡
本文最后更新于 1543 天前,其中的信息可能已经有所发展或是发生改变。

前段时间接到一个制作会员卡的单子,一开始挺好奇为什么要把一个会员天数要分开成卡,后来才发现,那个是以充值天数来的,然后需要以卡类型进行判断
那么卡分别为

  • 天卡·day
  • 周卡·week
  • 月卡·month
  • 季卡·season
  • 年卡·year

直接放算法

<?php

/**
 * Class vip_class 天数计算月卡
 * @author 院主网络科技团队
 * @link url www.berfen.com
 */
class vip_class{
    private $year=0;
    private $season=0;
    private $month=0;
    private $week=0;
    private $day=0;

    public function count($vip){
        switch ($this->rank($vip)){
            case 'day'://计算天卡
                $data=$this->day_count($vip);
                $this->day=$data['num'];
                break;

            case 'week'://计算周卡
                $data=$this->week_count($vip);
                $this->week=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'month'://计算月卡
                $data=$this->month_count($vip);
                $this->month=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'season'://计算季卡
                $data=$this->season_count($vip);
                $this->season=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='month'){
                    $data=$this->month_count($int);
                    $this->month=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'year'://计算年卡
                $data=$this->year_count($vip);
                $this->year=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='season'){
                    $data=$this->season_count($int);
                    $this->season=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='month'){
                    $data=$this->month_count($int);
                    $this->month=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;
        }
        $data=array(
            'year'=>$this->year
            ,'season'=>$this->season
            ,'month'=>$this->month
            ,'week'=>$this->week
            ,'day'=>$this->day
        );
        return $data;
    }

    private function year_count($int){//计算总出现的年卡
        $year=1;
        $int=$int-365;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='year'){
                $int=$int-365;
                $year++;
            }else{
                continue;
            }
        }
        $data['num']=$year;
        $data['int']=$int;
        return $data;
    }

    private function season_count($int){//计算总出现的季卡
        $season=1;
        $int=$int-90;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='season'){
                $int=$int-90;
                $season++;
            }else{
                continue;
            }
        }
        $data['num']=$season;
        $data['int']=$int;
        return $data;
    }

    private function month_count($int){//计算总出现的月卡
        $month=1;
        $int=$int-30;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='month'){
                $int=$int-30;
                $month++;
            }else{
                continue;
            }
        }
        $data['num']=$month;
        $data['int']=$int;
        return $data;
    }

    private function week_count($int){//计算总出现的周卡
        $week=1;
        $int=$int-7;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='week'){
                $int=$int-7;
                $week++;
            }else{
                continue;
            }
        }
        $data['num']=$week;
        $data['int']=$int;
        return $data;
    }

    private function day_count($int){//计算总出现的天卡
        $day=1;
        $int--;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='day'){
                $int--;
                $day++;
            }else{
                continue;
            }
        }
        $data['num']=$day;
        $data['int']=$int;
        return $data;
    }

    private function rank($score)
    {
        switch (true)
        {
            case $score >= 365:   return 'year';
            case $score >= 90:   return 'season';
            case $score >= 30:   return 'month';
            case $score >= 7:    return 'week';
            case $score >= 1:     return 'day';
            default:             return false;
        }
    }
}

代码有点复杂,没事,多看几遍其中的关系,就知道了
我们先看最后一个函数

private function rank($score)
    {
        switch (true)
        {
            case $score >= 365:   return 'year';
            case $score >= 90:   return 'season';
            case $score >= 30:   return 'month';
            case $score >= 7:    return 'week';
            case $score >= 1:     return 'day';
            default:             return false;
        }
    }

这个是一个判断一个数值是什么卡
如89,那就是月卡,然后通过月卡判断的循环次数就可以得到有几张卡了

调用示例

include 'vip_class.php';//必须引用这个类
$vip_class=new vip_class();//使用函数
$vip=896;//购买的天数
$data=$vip_class->count($vip);//调用函数返回数据

返回示例

首先,上面的$data是一个数据变量,我们只需要这样取出

echo $data['day'];

就可以得到天卡多少张了

年卡2张
季卡1张
月卡2张
周卡2张
天卡2张
知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇