{"id":76,"date":"2012-11-27T18:58:13","date_gmt":"2012-11-27T10:58:13","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2012-11-27\/php%25E5%25AE%259A%25E4%25B9%2589%25E6%2597%25B6%25E9%2597%25B4%25E6%258E%25A7%25E4%25BB%25B6"},"modified":"2013-04-20T17:24:52","modified_gmt":"2013-04-20T09:24:52","slug":"php%e5%ae%9a%e4%b9%89%e6%97%b6%e9%97%b4%e6%8e%a7%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=76","title":{"rendered":"php\u5b9a\u4e49\u65f6\u95f4\u63a7\u4ef6"},"content":{"rendered":"<p>php \u7f16\u5199\u65f6\u95f4\u63a7\u4ef6\u5c0f\u4f8b\u5b50\uff1a<\/p>\n<p><!--more--><\/p>\n<pre config=\"brush:php;toolbar:false;\">&lt;?php\n  \n\/** \n * @author youthflies\n * \n * \n *\/\nclass Calendar\n{\n  \n    \/\/TODO - Insert your code here\n    private $year;      \/\/\u5f53\u524d\u5e74\u4efd\n    private $month;     \/\/\u5f53\u524d\u6708\u4efd\n    private $days;      \/\/\u5f53\u524d\u6708\u4efd\u4e00\u5171\u591a\u5c11\u5929\n    private $start_weekday;     \/\/\u5f53\u524d\u6708\u4efd\u7b2c\u4e00\u5929\u662f\u5468\u51e0\n  \n    function __construct()\n    {\n      \n        \/\/TODO - Insert your code here\n        $this-&gt;year = isset($_GET[&quot;year&quot;]) ? $_GET[&quot;year&quot;] : date(&quot;Y&quot;);\n        $this-&gt;month = isset($_GET[&quot;month&quot;]) ? $_GET[&quot;month&quot;] : date(&quot;m&quot;);\n        $this-&gt;start_weekday = date(&quot;w&quot;, mktime(0, 0, 0, $this-&gt;month, 1, $this-&gt;year));\n        $this-&gt;days = date(&quot;t&quot;, mktime(0, 0, 0, $this-&gt;month, 1, $this-&gt;year));\n          \n    }\n  \n    \/**\n     * \n     *\/\n    function __destruct()\n    {\n      \n        \/\/TODO - Insert your code here\n    }\n      \n    \/**\n     * \u9b54\u672f\u65b9\u6cd5__toString()\u7528\u6765\u6253\u5370\u65e5\u5386\n     *\/\n    function __toString()\n    {\n        $out = '&lt;table align=&quot;center&quot;&gt;';\n        $out .= $this-&gt;chageDate();\n        $out .= $this-&gt;weeksList();\n        $out .= $this-&gt;daysList();\n        $out .= '&lt;\/table&gt;';\n        return $out;\n    }\n      \n    \/**\n     * weekList()\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5370\u5468\u5217\u8868\n     *\/\n    private function weeksList()\n    {\n        $week = array('\u65e5', '\u4e00', '\u4e8c', '\u4e09', '\u56db', '\u4e94', '\u516d');\n        $out = '&lt;tr&gt;';\n        for($i = 0; $i&lt;=count($week)-1; $i++)\n            $out .= '&lt;th class=&quot;fontb&quot;&gt;' . $week[$i] . '&lt;\/th&gt;';\n        $out .= &quot;&lt;\/tr&gt;&quot;;\n        return $out;\n    }\n      \n    \/**\n     * daysList()\u51fd\u6570\uff0c\u8f93\u5165\u65e5\u5217\u8868\n     *\/\n    private function daysList()\n    {\n        $j = 0; \/\/\u63a7\u5236\u6bcf\u4e00\u884c\u53ea\u8f93\u51fa7\u6b21\n        $out = '&lt;tr&gt;';\n        for($i = 0; $i&lt;=$this-&gt;start_weekday-1; $i++)\n        {\n            $out .= '&lt;td&gt; &amp;nbsp ' . '&lt;\/td&gt;';\n            $j++;\n        }\n        for($i=0; $i&lt;=$this-&gt;days-1; $i++)\n        {\n            if ($j%7==0)\n                $out .= '&lt;\/tr&gt;&lt;tr&gt;';\n            \/\/\u7a81\u51fa\u663e\u793a\u5f53\u524d\u65e5\n            if($i == date(&quot;d&quot;, time()))\n                $out .= '&lt;td bgcolor=&quot;blue&quot;&gt;' . ($i+1) . '&lt;\/td&gt;';\n            else\n                $out .= '&lt;td&gt;' . ($i+1) . '&lt;\/td&gt;';\n            $j++;\n        }\n        \/\/\u5269\u4f59\u7528\u7a7a\u683c\u8865\u9f50\n        while($j%7!=0)\n        {\n            $out .= '&lt;td&gt;' . ' ' .'&lt;\/td&gt;';\n            $j++;\n        }\n        return $out;\n    }\n      \n    \/**\n     * prevYear()\u51fd\u6570\u6765\u64cd\u4f5c\u7528\u6237\u70b9\u51fb\u4e0a\u4e00\u5e74\u7684\u52a8\u4f5c\n     *\/\n    private function prevYear($year, $month)\n    {\n        $year--;\n        if($year &lt;1970)\n            $year = 1970;\n        return &quot;year={$year}&amp;month={$month}&quot;;\n    }\n      \n    \/**\n     * prevMonth()\u51fd\u6570\u6765\u64cd\u4f5c\u7528\u6237\u70b9\u51fb\u4e0a\u4e00\u4e2a\u6708\u7684\u52a8\u4f5c\n     *\/\n    private function prevMonth($year, $month)\n    {\n        if($month &lt;= 1)\n        {\n            $year--;\n            if($year&lt;1970)\n            {\n                $month = 1;\n                $year = 1970;\n            }\n            else\n                $month = 12;\n        }\n        else\n            $month--;\n        return &quot;year={$year}&amp;month={$month}&quot;;\n    }\n      \n    \/**\n     * \u4e0b\u9762\u662f\u5904\u7406\u7528\u6237\u70b9\u51fb\u4e0b\u4e00\u4e2a\u6708\u548c\u4e0b\u4e00\u5e74\u7684\u52a8\u4f5c\n     *\/\n    private function nextYear($year, $month)\n    {\n        $year++;\n        if($year &gt;= 2038)\n            $year = 2038;\n        return &quot;year={$year}&amp;month={$month}&quot;;\n    }\n    private function nextMonth($year, $month)\n    {\n        if($month &gt;= 12)\n        {\n            $year++;\n            if($year == 2039)\n            {\n                $year = 2038;\n                $month = 12;\n            }\n            else\n                $month = 1;         \n        }\n        else\n            $month++;\n        return &quot;year={$year}&amp;month={$month}&quot;;\n    }\n      \n    \/**\n     * changeDate()\u51fd\u6570\u6267\u884c\u7528\u6237\u8c03\u6574\u5e74\u4efd\u548c\u6708\u4efd\n     *\/\n    private function chageDate($url = &quot;Calendar.php&quot;)\n    {\n        \/\/\u5b9a\u4e49\u8c03\u6574\u5e74\u4efd\u548c\u6708\u4efd\u7684\u5feb\u6377\u952e\n        $out = '&lt;tr&gt;';\n        $out .= '&lt;td&gt;&lt;a href=&quot;' . $url . '?' . $this-&gt;prevYear($this-&gt;year, $this-&gt;month) . '&quot;&gt;' . '&lt;&lt;' . '&lt;\/a&gt;&lt;\/td&gt;';\n        $out .= '&lt;td&gt;&lt;a href=&quot;' . $url . '?' . $this-&gt;prevMonth($this-&gt;year, $this-&gt;month) . '&quot;&gt;' . '&lt;' . '&lt;\/a&gt;&lt;\/td&gt;';\n        $out .= '&lt;td colspan=&quot;3&quot;&gt;';\n        $out .= '&lt;form&gt;';\n        $out .= '&lt;select name=&quot;year&quot; onchange=&quot;window.location='' . $url . '?year='+this.options[selectedIndex].value+'&amp;month=' . $this-&gt;month . ''&quot;&gt;';\n        for($sy=1970; $sy &lt;=2038; $sy++)\n        {\n            $selected = ($sy==$this-&gt;year) ? &quot;selected&quot; : &quot;&quot;;\n            $out .= '&lt;option ' . $selected . ' value=&quot;' . $sy .'&quot;&gt;' . $sy . '&lt;\/option&gt;';\n        }\n        $out .= '&lt;\/select&gt;';\n        \/\/$out .= '&lt;\/td&gt;';\n        \/\/$out .= '&lt;td colspan=&quot;2&quot;&gt;';\n        $out .= '&lt;select name=&quot;month&quot; onchange=&quot;window.location='' . $url . '?year=' . $this-&gt;year . '&amp;month='+this.options[selectedIndex].value&quot;&gt;';\n        for($sm=1; $sm&lt;=12; $sm++)\n        {\n            $selectedMonth = ($sm==$this-&gt;month)? &quot;selected&quot; : &quot;&quot;;\n            $out .= '&lt;option ' . $selectedMonth . ' value=&quot;' . $sm . '&quot;&gt;' . $sm . '&lt;\/option&gt;';\n        }\n        $out .='&lt;\/select&gt;';\n        $out .= '&lt;\/form&gt;';\n        $out .= '&lt;\/td&gt;';\n          \n        $out .= '&lt;td&gt;&lt;a href=&quot;' . $url . '?' . $this-&gt;nextYear($this-&gt;year, $this-&gt;month) . '&quot;&gt;' . '&gt;&gt;' . '&lt;\/a&gt;&lt;\/td&gt;';\n        $out .= '&lt;td&gt;&lt;a href=&quot;' . $url . '?' . $this-&gt;nextMonth($this-&gt;year, $this-&gt;month) . '&quot;&gt;' . '&gt;' . '&lt;\/a&gt;&lt;\/td&gt;';\n        $out .= '&lt;\/tr&gt;';\n        return $out;\n    }\n}\n  \n  \necho '&lt;html&gt;';\necho '&lt;head&gt;';\necho '&lt;meta http-equiv=Content-Type content=&quot;text\/html;charset=utf-8&quot;&gt;';\necho '&lt;style&gt;\n    table { border:1px solid #050;}\n    .fontb { color:wihte; background:blue; }\n    th {    width:35px; }\n    td, th  {   width: 40px; height:30px; text-aligh:center;    }\n    form    { margin:0px; padding:0px;  }\n    &lt;\/style&gt;';\necho '&lt;title&gt;\u65e5\u671f\u63a7\u4ef6&lt;\/title&gt; &lt;\/head&gt;';\necho '&lt;body&gt;';\n        $cal = new Calendar();\n        echo $cal;\necho '&lt;\/body&gt; &lt;\/html&gt;';\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>php \u7f16\u5199\u65f6\u95f4\u63a7\u4ef6\u5c0f\u4f8b\u5b50\uff1a<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[24,7],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-coding","tag-php","tag-7"],"views":2915,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76"}],"version-history":[{"count":2,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":386,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions\/386"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}