php根据指定时间日历,php学习笔记(十三)时间处理与日历的实现
/**
* 時間和日期
* 1.unix時間戳
* 以32位整數表示的格林威治時間
* 時間戳是從1970年1月1日0點0分0秒到現在的開始計時
* 方便我們計算使用
* 處理的時間在1902年-2038年之內是有效的(時間戳不能為負數)
* 2.在php中獲取日期和時間
* time()
* getDate()
* getTimeOfDay()
* date_sunrise()
* date_sunset()
* 3.格式化輸出
* date($str,[timestamp]);(查看使用的符號)
* 4.將日期和時間編程unix時間戳
* mktime($hour, $minute, $second, $month, $day, $year, $is_dst)
* 如果只想傳喚日期,則前三個參數傳入0即可
* 5.修改php的默認時區
* 最好服務器的時間,沒半個小時與世界時間同步一下
* php.ini 的 date.timezone
* 或者
* ini_set("date.timezone", "ETC/GMT-8");
* date_default_timezone_set("ETC/GMT-8");(php5特性)
* 6.使用微妙計算php腳本的執行時間
* microtime();(帶有boolean參數的是php5的新特性)
*
* 示例:日歷類
*/
//設置時區(參數去查看php手冊的參數列表)
//date_default_timezone_set("ETC/GMT-8");
ini_set("date.timezone", "ETC/GMT-8");
//獲取時間
$time = time();
echo $time."
";
$date = @getDate();
var_dump($date)."
";
$timeofday = @getTimeOfDay();
var_dump($timeofday)."
";
$date_sunrise = @date_sunrise();
echo $date_sunrise."
";
$date_sunset = @date_sunset();
echo $date_sunset."
"."
";
//格式化日期和時間
echo @date("Y-m-d H:i:s",time())."
";
echo @date("Y-m-d H:i:s")."
"."
";
//轉換unix時間戳
echo @date("Y-m-d H:i:s",@mktime(12,29,22))."
";
echo @date("Y-m-d H:i:s",@mktime(0,0,0,12,2002))."
";
echo @date("Y-m-d H:i:s",@mktime(0,0,0,12,36,2007))."
";
echo @date("Y-m-d H:i:s",@mktime(0,0,0,1,1,99))."
";
//計算年齡方法
@list($year,$month,$day)=@explode("-", $_GET["birthday"]);
$agestamp = time()-@mktime(0,0,0,$month,$day,$year);
$age = $agestamp/(60*60*24*365);
echo $age."
";
//使用微妙計算php的時間
class Time{
private $starTime;
private $stopTime;
function __construct(){
$this->starTime = 0;
$this->stopTime = 0;
}
function start(){
$this->starTime=microtime(true);
}
function stop(){
$this->stopTime=microtime(true);
}
function speat(){
//四舍五入四位
return round($this->stopTime-$this->starTime,4);
}
}
$timer = new Time();
$timer->start();
for ($i=0;$i<10000;$i++){}
$timer->stop();
echo $timer->speat();
include 'calendar.class.php';
$calendar = new Calendar();
$calendar->out();
?>
table{
border:1px;
}
.fonth{
color:white;
background:blue;
}
th{
width:30px;
}
form{
margin: 0px;
padding: 0px;
}
class Calendar{
//當月的第一天對應周幾
private $star_weekday;
//當月的天數
private $days;
private $year;
private $month;
function __construct(){
$this->year=isset($_GET["year"])?$_GET["year"]:date("Y");
$this->month=isset($_GET["month"])?$_GET["month"]:date("m");
$this->star_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 "
$this->changeDate("date.php");
$this->weekList();
$this->dayList();
echo "
";}
/**
* 循環輸出星期
* Enter description here ...
*/
private function weekList(){
$week = array("日","一","二","三","四","五","六");
echo "
";for ($i=0;$i
echo "
".$week[$i]."";}
echo "
";}
/**
* 循環輸出天數
* Enter description here ...
*/
private function dayList() {
echo "
";//輸出空格
for ($j=0;$jstar_weekday;$j++){
echo "
";}
for ($i=1;$idays;$i++){
$j++;
if ($i==date("d"))
echo "
".$i."";else
echo "
".$i."";if ($j%7==0) {
echo "
";;}
}
while ($j%7==0){
echo "
";$j++;
}
echo "
";}
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=$month-1;
}
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=$year+1;
if ($year>2038)$year = 2038;
$month=1;
}else{
$month=$month+1;
}
return "year={$year}&month={$month}";
}
private function changeDate($url=""){
echo "
";echo "
"."<";echo "
"."";echo "
";echo "
";echo '';
for ($y = 1970;$y<2039;$y++){
$selected = ($y==$this->year)?"selected":"";
echo "".$y."";
}
echo "";
echo '';
for ($m = 1;$m<13;$m++){
$selected = ($m==$this->month)?"selected":"";
echo "".$m."";
}
echo "";
echo "
";echo "
";echo "
".">"."";echo "
".">>"."";echo "
";}
}
?>
源碼下載:
php教程學習筆記
總結
以上是生活随笔為你收集整理的php根据指定时间日历,php学习笔记(十三)时间处理与日历的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php dropdownlist,遇到d
- 下一篇: php js vbs,VBScript版