php计算会员卡

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

  • 天卡·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张
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joshua Burgin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值