Code tìm thứ trong tuần – PHP (tt – phần 1)
Viet Matrix Revolutions nay tiếp tục bài PHP cơ bản
Yêu cầu : Viết 1 ứng dụng khi nhập ngày – tháng – năm nhấn nút button thì ứng dụng xuất ra ngày – tháng – năm và thứ tương ứng
Hướng dẫn:
Tạo 3 text field để nhập ngày tháng năm , Tạo thêm 1 text field để xuất kết quả với giá trị là readonly=”true” [không cho nhập giá trị ]
Khai báo biến $jd và dùng hàm
cal_to_jd(CAL_GREGORIAN,$ngay,$thang,nam);
$day=jddayofweek($jd,0)
[giá trị 0 là để tính từ chủ nhật đến thứ 7 ứng với 0 ->6]
Sau đó đọc thứ trong tuần bằng cấu trúc switch … case
switch($day)
{
case=0:
$thu=”Chủ Nhật”;
break;
.. .. . .
}
Kết quả do Viet Matrix Revolutions thực hiện

Tìm thứ trong tuần - Viet Matrix Revolutions
Sau đây là code
===================================
<!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”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Thứ Trong Tuần</title>
<style type=”text/css”>
<!–
.style1 {color: #FFFFFF}
.style2 {
color: #000000;
font-weight: bold;
}
–>
</style>
</head>
<body>
<?php
/*
Viet Matrix Revolutions – AlexBTP – Anderson_neo , trina_lan and member Viet Matrix Revolutions
*/
//xét giá trị nhập vào
if(isset($_POST["ngay"]) && isset($_POST["thang"]) && isset($_POST["nam"]))
{
$ngay=$_POST["ngay"];
$thang=$_POST["thang"];
$nam=$_POST["nam"];
$jd=cal_to_jd(CAL_GREGORIAN,$thang,$ngay,$nam);
$day=jddayofweek($jd,0);
switch($day)
{
case 0:
$thu=”Chủ Nhật”;
break;
case 1:
$thu=”Thứ Hai”;
break;
case 2:
$thu=”Thứ Ba”;
break;
case 3:
$thu=”Thứ Tư”;
break;
case 4:
$thu=”Thứ Năm”;
break;
case 5:
$thu=”Thứ Sáu”;
break;
case 6:
$thu=”Thứ 7″;
break;
//Vì mod bằng 0
}
$xuat_thu=”Ngày $ngay Tháng $thang Năm $nam Là $thu”;
}
?>
<form id=”form1″ name=”form1″ method=”post” action=”">
<table width=”500″ border=”0″ align=”center” bgcolor=”#FF99FF”>
<tr>
<td colspan=”5″ align=”center” bgcolor=”#CC3366″><h1 class=”style1″>Tìm Thứ Trong Tuần </h1></td>
</tr>
<tr>
<td width=”130″><span class=”style2″>Ngày-Tháng-Năm</span></td>
<td width=”72″><label>
<input name=”ngay” type=”text” id=”ngay” value=”<?php echo $_POST["ngay"];?>” size=”10″ />
</label></td>
<td width=”68″><label>
<input name=”thang” type=”text” id=”thang” value=”<?php echo $_POST["thang"];?>” size=”10″ />
</label></td>
<td width=”60″><label>
<input name=”nam” type=”text” id=”nam” value=”<?php echo $_POST["nam"];?>” size=”10″ />
</label></td>
<td width=”148″><label>
<input type=”submit” name=”Submit” value=”Tìm” />
</label></td>
</tr>
<tr>
<td colspan=”5″ align=”center”><label>
<input name=”xuat_thu” type=”text” id=”xuat_thu” style=”background-color:#FFFFCC” value=”<?php echo $xuat_thu;?>” size=”50″ readonly=”true”/>
<strong>Viet Matrix Revolutions </strong></label></td>
</tr>
</table>
</form>
</body>
</html>
===================================
Toàn quyền thuộc về Viet Matrix Revolutions – Ghi rõ nguồn khi phát hành lại thông tin này
Anderson_neo – AlexBTP – Trina_Lan and member Viet Matrix Revolutions









Phản hồi gần đây