一個php日曆程序是什麼,php日曆表代碼

本文目錄一覽:

如何用php做日曆

具體代碼如下:

?php

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, ”);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

echo $file_contents;

?

PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。

它可以比CGI或者Perl更快速地執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標準通用標記語言下的一個應用)文檔中去執行,

執行效率比完全生成HTML標記的CGI要高許多;

PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

如何用PHP製作日曆

calendar.class.php

 代碼如下:

 ?php

class Calendar {

  private $year; //當前的年

  private $month; //當前的月

  private $start_weekday; //當月的第一天對應的是周幾

  private $days; //當前月一共多少天

 

  function __construct(){

   $this-year=isset($_GET[“year”]) ? $_GET[“year”] : date(“Y”);

   $this-month=isset($_GET[“month”]) ? $_GET[“month”] : date(“m”);

 

   $this-start_weekday=date(“w”, mktime(0, 0, 0, $this-month, 1, $this-year));

   $this-days=date(“t”, mktime(0, 0, 0, $this-month, 1, $this-year));

  }

 

  function out(){

   echo ‘table align=”center”‘;

   $this-chageDate(“test.php”);

   $this-weeksList();

   $this-daysList();

   echo ‘/table’;

  }

 

  private function weeksList(){

   $week=array(‘日’,’一’,’二’,’三’,’四’,’五’,’六’);

 

   echo ‘tr’;

   for($i=0; $icount($week); $i++)

    echo ‘th class=”fontb”‘.$week[$i].’/th’;

 

   echo ‘/tr’;

  }

 

  private function daysList(){

   echo ‘tr’;

   //輸出空格(當前一月第一天前面要空出來)

   for($j=0; $j$this-start_weekday; $j++)

    echo ‘td /td’;

 

 

   for($k=1; $k=$this-days; $k++){

    $j++;

    if($k==date(‘d’))

     echo ‘td class=”fontb”‘.$k.’/td’;

    else

     echo ‘td’.$k.’/td’;

 

    if($j%7==0)

     echo ‘/trtr’;

 

   }

 

   //後面幾個空格

   while($j%7!==0){

    echo ‘td /td’;

    $j++;

   }

 

   echo ‘/tr’;

  }

 

  private function prevYear($year, $month){

   $year=$year-1;

 

   if($year  1970)

    $year = 1970;

 

   return “year={$year}month={$month}”; 

  }

 

 

  private function prevMonth($year, $month){

   if($month == 1) {

    $year = $year -1;

 

    if($year  1970)

     $year = 1970;

 

    $month=12;

   }else{

    $month–;

   }

 

   return “year={$year}month={$month}”; 

  }

 

 

  private function nextYear($year, $month){

   $year = $year + 1;

 

   if($year  2038)

    $year = 2038;

 

   return “year={$year}month={$month}”; 

  }

 

 

  private function nextMonth($year, $month){

   if($month==12){

    $year++;

 

    if($year  2100)

     $year=2100;

 

    $month=1;

   }else{

    $month++;

   }

   

 

   return “year={$year}month={$month}”; 

  }

 

  private function chageDate($url=””){

   echo ‘tr’;

   echo ‘tda href=”?’.$this-prevYear($this-year, $this-month).'”‘.”.’/a/td’;

   echo ‘tda href=”?’.$this-prevMonth($this-year, $this-month).'”‘.”.’/a/td’;

   echo ‘td colspan=”3″‘;

   echo ‘form’;

   echo ‘select name=”year” onchange=”window.location=”.$url.’?year=’+this.options[selectedIndex].value+’month=’.$this-month.””‘;

   for($sy=1970; $sy = 2100; $sy++){

    $selected = ($sy==$this-year) ? “selected” : “”;

    echo ‘option ‘.$selected.’ value=”‘.$sy.'”‘.$sy.’/option’;

   }

   echo ‘/select’;

   echo ‘select name=”month”  onchange=”window.location=”.$url.’?year=’.$this-year.’month=’+this.options[selectedIndex].value”‘;

   for($sm=1; $sm=12; $sm++){

    $selected1 = ($sm==$this-month) ? “selected” : “”;

    echo ‘option ‘.$selected1.’ value=”‘.$sm.'”‘.$sm.’/option’;

   }

   echo ‘/select’;

   echo ‘/form’; 

   echo ‘/td’;

 

 

   echo ‘tda href=”?’.$this-nextYear($this-year, $this-month).'”‘.”.’/a/td’;

   echo ‘tda href=”?’.$this-nextMonth($this-year, $this-month).'”‘.”.’/a/td’;

   echo ‘/tr’;

  }

 

 }

?

 

 

 

 test.php

 

  代碼如下:

 style

 table {

  border:1px solid #050;

 }

 

 .fontb {

  color:white;

  background:blue;

 }

 

 

 th {

  width:30px;

 }

 

 td,th {

  height:30px;

  text-align:center;

 

 }

 form {

  margin:0px;

  padding:0px;

 }

/style

?php

 include “calendar.class.php”;

 

 $calendar=new Calendar;

 

 $calendar-out();

?

PHP如何生成一個指定年份一整年的日曆

從你的描述看來,只需要知道指定年份的每個月的天數和每天對應的周次即可。

PHP中 Calendar 函數可以實現第一步:

int cal_days_in_month ( int $calendar , int $month , int $year )

剩下的就是寫循環遍歷所有月,按月生成日曆了。

原創文章,作者:YIMZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/145139.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YIMZ的頭像YIMZ
上一篇 2024-10-26 11:56
下一篇 2024-10-26 11:56

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29

發表回復

登錄後才能評論