自动获取节日日期并显示灯笼效果的PHP源码

2021年03月3日

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/3724.html
关键字:网站灯笼 春节灯笼 网站节日灯笼

导读:
shell获取每年农历节日的日期:https://www.myzhenai.com.cn/post/3711.html
为网站增加春节灯笼效果:https://www.myzhenai.com.cn/post/3645.html

因为之前我在网上找到了一个网站灯笼效果的CSS源码,这源码是使用CSS重画两个灯笼的样式出来,然后再灯笼上显示节日,但是使用起来有些麻烦,因为要不停的修改网站后台或源文件,我就在想,可不可以通过PHP来获取节日日期,并且自动显示,通过几天的折腾,也完整折腾出来源码了,在这里要感谢深圳的邱斌兄弟,是他给了我一些指导和耐心的帮忙调试错误。写出来源码很简单,但是因为编写源码的环境改变了,所以出现了一些低级的错误。

1、PHP中括号()出错,因为在写 jieri_date()函数的时候,调用了一个get_yunxiao()的函数,但是我却错误的把后边的括号写成全角字符了
2、shell中在curl的时候,地址中的&符号需要用\进行转义,我就是因为没有对符号进行转义,调试了一整天
3、PHP读取shell脚本生成日期模块的调试出错

还好,现在这些问题都解决了,因为我以前是在Centos桌面里写源码,所以没有出现过这样的低级错误,但是现在换成Windows7下用phpstorm来写,但是在输入法切换上却出现了这样的低级错误,白白浪费了几天的时间来调试错误。

言归正传,现在来演示达到效果需要的几步工作。

1、注册聚合数据平台,使用 http://v.juhe.cn/calendar/year?year=2018&key=您申请的appKey 来获取当年节日时间,我们这里需要获取非固定的节日时间,例如:春节、元宵、端午、中秋等农历节日,像国庆、元旦、圣诞、情人等固定新历时间的不需要。
2、准备好phpstorm和UltraEdit编辑器
3、先把shell脚本保存为.sh后缀的文件上传到服务器/home/目录
4、下载你网站目录中的functions.php和header.php两个源文件到本地,使用phpstorm或UltraEdit打开,然后把以下的源码复制到functions.php中的相关位置,然后在header.php相关位置中添加 这一句来进行调用即可。
5、将functions.php和header.php两个源文件上传到网站目录中覆盖原文件即可。
6、演示:https://jiayu.mybabya.com/jieri.php

SHELL源码

    #!/usr/bin/env bash
    # !/bin/bash
    ############################################################################################################################################
    #
    # This is a lottery number automatic generation tool
    # Autor: RucLinux
    # Web: https://www.myzhenai.com.cn/ https://www.myzhenai.com/ https://mangshe.python.hn.cn/ https://jiayu.mybabya.com/
    #  
    #
    #############################################################################################################################################
var=$(date +%Y)
fen=$(curl -s http://v.juhe.cn/calendar/year?year=$var\&key=youappKey | awk -F "[" '{print $2}' | awk -F "]" '{print $1}' | sed 's/\\//g' | sed 's/\"//g' | sed 's/}\,/}/g' | sed 's/\,/\": /g' | sed 's/startday:/\"/g' | sed 's/}/\",\\n/g' | sed "s/name:/\"/g" | sed "s/{/  /g" | sed '/^[[:space:]]*$/d')
echo -e "{" > /var/www/jieri.log
echo -e "${fen}" >> /var/www/log
echo -e "}" >> /var/www/jieri.log

注:这里有几点需要说明

1、youappKey这个一定要替换成你在聚合数据上申请的免费固定key,为什么要使用shell来获取这个接口数据呢,因为免费版本的1个月只有1000次免费使用,如果在php里调用这个接口的话,那么将很快就超出次数了。

2、echo -e “${fen}” 这里必须要使用双引号引住

3、将上边的源码保存为.sh文件,然后在crond里设置成每月运行即可

/*# This is a festival code automatic generation tool
  # Autor: RucLinux # Web: https://www.myzhenai.com.cn/ https://www.myzhenai.com/ https://mangshe.python.hn.cn/ https://jiayu.mybabya.com/
*/
    /** 输出包含日期的css代码 $rixiid 变量是春节、端午、元宵、中秋等中文*/
    function CSS_code($rixiid)
    {
        $cssid = <<<Kof
<div class="deng-box">
    <div class="deng">
        <div class="xian"></div>
        <div class="deng-a">
            <div class="deng-b"><div class="deng-t">$rixiid</div></div>
        </div>
        <div class="shui shui-a"><div class="shui-c"></div><div class="shui-b"></div></div>
    </div>
</div>
 
<div class="deng-box1">
    <div class="deng">
        <div class="xian"></div>
        <div class="deng-a">
            <div class="deng-b"><div class="deng-t">快乐</div></div>
        </div>
        <div class="shui shui-a"><div class="shui-c"></div><div class="shui-b"></div></div>
    </div>
</div>

<style>
    .deng-box {
        position: fixed;
        top: -30px;
        left: 220px;
        z-index: 9999;
        pointer-events: none;
    }
     
    .deng-box1 {
        position: fixed;
        top: -30px;
        right: 222px;
        z-index: 9999;
        pointer-events: none;
    }
     
    .deng-box1 .deng {
        position: relative;
        width: 120px;
        height: 90px;
        margin: 50px;
        background: #d8000f;
        background: rgba(216, 0, 15, 0.8);
        border-radius: 50% 50%;
        -webkit-transform-origin: 50% -100px;
        -webkit-animation: swing 5s infinite ease-in-out;
        box-shadow: -5px 5px 30px 4px rgba(252, 144, 61, 1);
    }
     
    .deng {
        position: relative;
        width: 120px;
        height: 90px;
        margin: 50px;
        background: #d8000f;
        background: rgba(216, 0, 15, 0.8);
        border-radius: 50% 50%;
        -webkit-transform-origin: 50% -100px;
        -webkit-animation: swing 3s infinite ease-in-out;
        box-shadow: -5px 5px 50px 4px rgba(250, 108, 0, 1);
    }
     
    .deng-a {
        width: 100px;
        height: 90px;
        background: #d8000f;
        background: rgba(216, 0, 15, 0.1);
        margin: 12px 8px 8px 10px;
        border-radius: 50% 50%;
        border: 2px solid #dc8f03;
    }
     
    .deng-b {
        width: 45px;
        height: 90px;
        background: #d8000f;
        background: rgba(216, 0, 15, 0.1);
        margin: -2px 8px 8px 26px;
        border-radius: 50% 50%;
        border: 2px solid #dc8f03;
    }
     
    .xian {
        position: absolute;
        top: -20px;
        left: 60px;
        width: 2px;
        height: 20px;
        background: #dc8f03;
    }
     
    .shui-a {
        position: relative;
        width: 5px;
        height: 20px;
        margin: -5px 0 0 59px;
        -webkit-animation: swing 4s infinite ease-in-out;
        -webkit-transform-origin: 50% -45px;
        background: #ffa500;
        border-radius: 0 0 5px 5px;
    }
     
    .shui-b {
        position: absolute;
        top: 14px;
        left: -2px;
        width: 10px;
        height: 10px;
        background: #dc8f03;
        border-radius: 50%;
    }
     
    .shui-c {
        position: absolute;
        top: 18px;
        left: -2px;
        width: 10px;
        height: 35px;
        background: #ffa500;
        border-radius: 0 0 0 5px;
    }
     
    .deng:before {
        position: absolute;
        top: -7px;
        left: 29px;
        height: 12px;
        width: 60px;
        content: " ";
        display: block;
        z-index: 999;
        border-radius: 5px 5px 0 0;
        border: solid 1px #dc8f03;
        background: #ffa500;
        background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
    }
     
    .deng:after {
        position: absolute;
        bottom: -7px;
        left: 10px;
        height: 12px;
        width: 60px;
        content: " ";
        display: block;
        margin-left: 20px;
        border-radius: 0 0 5px 5px;
        border: solid 1px #dc8f03;
        background: #ffa500;
        background: linear-gradient(to right, #dc8f03, #ffa500, #dc8f03, #ffa500, #dc8f03);
    }
     
    .deng-t {
        font-family: 华文行楷,Arial,Lucida Grande,Tahoma,sans-serif;
        font-size: 1.5rem;
        color: #ffa500;
        font-weight: bold;
        line-height: 42px;
        text-align: center;
        width: 25px;  
        margin: 0 auto;  
         
    }
     
    .night .deng-t, 
    .night .deng-box, 
    .night .deng-box1 {
        background: transparent !important;
    }
     
    @-moz-keyframes swing {
        0% {
            -moz-transform: rotate(-10deg)
        }
     
        50% {
            -moz-transform: rotate(10deg)
        }
     
        100% {
            -moz-transform: rotate(-10deg)
        }
    }
     
    @-webkit-keyframes swing {
        0% {
            -webkit-transform: rotate(-10deg)
        }
     
        50% {
            -webkit-transform: rotate(10deg)
        }
     
        100% {
            -webkit-transform: rotate(-10deg)
        }
    }
</style>
Kof;
        return $cssid;
    }

    /** 这是将所有字符串编码转换成UTF-8 */
    function strToUtf8($str){
        $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
        if($encode == 'UTF-8'){
            return $str;
        }else{
            return mb_convert_encoding($str, 'UTF-8', $encode);
        }
    }

    /** 判断春节、中秋、端午等节日时间*/
    function panduan($jie)
    {
        $file = "jieri.log";
        if (file_exists($file)){
            $fp = file_get_contents($file);
               $hang = strToUtf8($fp);
               if (strpos($hang,$jie) !== false){
                    $left = "\": \"";
                    $rght = "\",";
                    $th = strpos($hang,$jie,0);
                    $t = strpos($hang,$left,$th);
                    $tx = strpos($hang,$rght,$t);
                    $bt = str_replace("\": \"","",substr($hang,$t,$tx-$t));
                }
        }
        return $bt;
    }


    /**
     * +----------------------------------------------------------
     * 功能:计算两个日期相差 年 月 日
     * +----------------------------------------------------------
     * @param date $date1 起始日期
     * @param date $date2 截止日期日期
     * +----------------------------------------------------------
     * @return array
     * +----------------------------------------------------------
     */
    function DiffDate($date1, $date2)
    {
        if (strtotime($date1) > strtotime($date2)) {
            $ymd = $date2;
            $date2 = $date1;
            $date1 = $ymd;
        }
        list($y1, $m1, $d1) = explode('-', $date1);
        list($y2, $m2, $d2) = explode('-', $date2);
        $y = $m = $d = $_m = 0;
        $math = ($y2 - $y1) * 12 + $m2 - $m1;
        $y = round($math / 12);
        $m = intval($math % 12);
        $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
        if ($d < 0) {
            $m -= 1;
            $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
        }
        $m < 0 && $y -= 1;
        return array($y, $m, $d);
    }

    /** 判断两个时间相差多少天 */
    function time_difference($time1, $time2)
    {
        /** time1是小的时间。time2是大的时间,用time1-time2 */
        $today = date_create($time1);
        $zhongqiu = date_create($time2);
        $diff = date_diff($today, $zhongqiu);
        $dayto = $diff->format("%a");
        return $dayto;
    }

    /** 计算元宵节日期,需要提供除夕时,在这个时间上加15天就是元宵节 */
    function get_yunxiao()
    {
        $date = date_create(panduan("除夕"));
        $yun = date_add($date, date_interval_create_from_date_string("15 days"));
        $yunxiao = date_format($yun, "Y-n-d");
        return $yunxiao;
    }

    /** 输出节日祝福信息 */
    function jie_date()
    {
        $dd = date("Y-n-d");
        $dc = date("Y-n-d", strtotime("+1 day"));

        if (time_difference(date("Y") . "-1-1", $dc) <= 2 || time_difference(date("Y") . "-1-1", date("Y-n-d")) <= 4) {
            echo CSS_code("新年");
        }
        if (time_difference(date("Y") . "-10-1", $dc) <= 2 || time_difference(date("Y") . "-10-1", date("Y-n-d")) <= 8) {
            echo CSS_code("国庆");
        }
        if (time_difference(date("Y") . "-2-14", $dc) <= 2 || time_difference(date("Y") . "-2-14", date("Y-n-d")) <= 2) {
            echo CSS_code("情人");
        }
        if (time_difference(date("Y") . "-3-1", $dc) <= 2 || time_difference(date("Y") . "-3-1", date("Y-n-d")) <= 2) {
            echo CSS_code("开心");
        }
        if (time_difference(date("Y") . "-12-24", $dc) <= 2 || time_difference(date("Y") . "-12-24", date("Y-n-d")) <= 2) {
            echo CSS_code("圣诞");
        }
        if (time_difference($dd, panduan("春节")) <= 7 || time_difference(panduan("春节"), $dd) <= 7) {
            echo Css_code("春节");
        }
        if (time_difference($dd, panduan("中秋")) <= 3 || time_difference(panduan("中秋"), $dd) <= 3) {
            echo Css_code("中秋");
        }
        if (time_difference($dd, panduan("端午")) <= 3 || time_difference(panduan("端午"), $dd) <= 3) {
            echo Css_code("端午");
        }
        if (time_difference($dd, get_yunxiao()) <= 2 || time_difference(get_yunxiao(), $dd) <= 2) {
            echo Css_code("元宵");
        }
    }


sicnature ---------------------------------------------------------------------
Your current IP address is: 44.222.104.49
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://myzhenai.com.cn/post/3724.html

2条评论

回复 JiaYu Blog 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注