duankang5882 2013-11-05 08:44
浏览 53
已采纳

创建一个PHP变量,每周更改背景的颜色

I would like to create a PHP variable which I can echo to change the color of the background weekly.

Here is what I had reached so far

<?php 
    // set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('EST');
    $today = date("l");
    if($today == "Sunday") 
        {
            $color = "#FEF0C5";
        }
    elseif($today == "Monday")
        {
            $color = "#FFFFFF";
        } 
    elseif($today == "Tuesday") 
        {
            $color = "#000000";
        } 
    elseif($today == "Wednesday")
        {
            $color = "#FFE0DD";
        } 
    elseif($today == "Thursday")
        {
            $color = "#E6EDFF";
        } 
    elseif($today == "Friday") 
        {
            $color = "#E9FFE6";
        } 
    else 
        {
    // Since it is not any of the days above it must be Saturday
            $color = "#F0F4F1";
        }
    print("<body bgcolor=\"$color\">
"); 
?>

I only managed to make the colors change daily, but i don't know how can I make the colors change weekly instead.

The second thing is that I need to make the colors of each first and last day of the month the color should be pink.

Any help would be very very much appreciated!

展开全部

  • 写回答

3条回答 默认 最新

  • doujiao8649 2013-11-05 08:54
    关注

    You can use this to change the color every week:

    $today = date("W");
    

    $today will be a value between 1 and 52, so you'll have every week of the year covered

    date("t") returns the number of days of the current month
    

    So to check if it's the first or last day, you can use this:

    $LastDayOfMonth = date("Y-m-t");
    $FirstDayOfMonth = date("Y-m-01");
    

    So to put it all together, you could do it like this:

    date_default_timezone_set('EST');
    $today = date("W");
    switch ($today) {
        case 1:
            $color = "the color you want";
            break;
        case 2:
            $color = "the color you want";
            break;
        case 3:
            $color = "the color you want";
            break;
        // All the other cases here...
    }
    
    $CurrentDate = date("Y-m-d");
    $LastDayOfMonth = date("Y-m-t");
    $FirstDayOfMonth = date("Y-m-1");
    
    if ($CurrentDate == $LastDayOfMonth || $CurrentDate == $FirstDayOfMonth ) {
        $color = "the pink rgb-code";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部