在一个网页上,根据用户的请求(选择),来显示不同的时间效果:
效果1:只显示年月日
效果2:只显示时分秒
效果3(默认效果)显示年月日时分秒;
第一
控制器:
Controller,是一个php文件,由浏览器直接请求(访问);
它需要做2件最核心的工作:
1,(根据请求),决定需要什么数据,并去调用模型文件(类),去获取该数据;
2,(根据请求),决定需要将数据显示在哪个视图文件中。
代码如下:
<?php
require './MyDateTime.class.php';//
//根据用户请求,以决定“获取”什么样的时间:
//效果1
if(!empty($_GET['f']) && $_GET['f'] == 'time'){
//$t = date("H:i:s");
$obj = new MyDateTime();
$t = $obj->GetTime();
}
//效果2
else if(!empty($_GET['f']) && $_GET['f'] == 'date'){
//$t = date("Y年m月d日");
$obj = new MyDateTime();
$t = $obj->GetDate();
}
else{//默认
//$t = date("Y年m月d日 H:i:s");
$obj = new MyDateTime();
$t = $obj->GetDateTime();
}
//表现(展示)该时间:
include './4mvc_time_demo1.html';
?>
第二步
模型:
Model,是一个php文件,不能直接请求,只能“被载入”而发挥作用。
它的核心工作只有一个:
(根据控制器的要求)去生产数据;
MyDateTime.class.php
<?php
class MyDateTime{
function GetDate(){
return date("Y年m月d日");
}
function GetTime(){
return date("H:i:s");
}
function GetDateTime(){
return date("Y年m月d日 H:i:s");
}
}
?>
第三步
视图:
View,是一个“伪html文件”(因为其中有极简单的php代码),它也不应由浏览器直接请求;
它的作用是:
结合html和css代码,显示相应的变量(数据)
4mvc_time_demo1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<meta name="keywords" content="关键字列表" />
<meta name="description" content="网页描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css">
.time{color:red; font-size:30px; background-color:yellow;}
</style>
<script type="text/javascript"></script>
</head>
<body>
<p align="right">
<a href="4mvc_time_demo1.php?f=date">形式1</a> |
<a href="4mvc_time_demo1.php?f=time">形式2</a> |
<a href="4mvc_time_demo1.php?f=datetime">形式3</a>
</p>
<div class="time">
<?php
echo $t;
?>
</div>
</body>
</html>