Two php date control instances and php date instances

Source: Internet
Author: User

Two php date control instances and php date instances

This example describes two php date control classes. Share it with you for your reference. The specific analysis is as follows:

Because I have to find two time and date controls for my work, I don't need to implement js as long as php is used, because I want to refer to the query operations. If you are interested, please refer to them, I used the second one myself, so the second one was modified.

Example 1: the code is as follows:
Copy codeThe Code is as follows: <? Php
Class Calendar
{
Var $ month;
Var $ year;
 
Function _ construct ($ year, $ month)
{
$ This-> year = $ year;
$ This-> month = $ month;
}
 
Function endday ()
{
$ Daydate = date ("d", mktime (0, 0, 0, $ this-> month, 35, $ this-> year ));
$ Endday = 35-$ daydate;
Return $ endday;
}
 
Function oneday_week ()
{
$ Oneday_week = date ("w", mktime (0, 0, 0, $ this-> month, 1, $ this-> year ));
Return $ oneday_week;
}
 
Function title_link ()
{
If (! Isset ($ this-> month )&&! Isset ($ this-> year ))
{
$ This-> year = date ("Y ");
$ This-> month = date ("m ");
}

$ Lastmonth = $ this-> month-1;
$ Nextmonth = $ this-> month + 1;
$ Lastyear = $ this-> year;
$ Nextyear = $ this-> year;
 
If ($ this-> month <= 1)
{
$ Lastmonth = 12;
$ Nextmonth = $ this-> month + 1;
$ Lastyear = $ this-> year-1;
$ Nextyear = $ this-> year;
}
Elseif ($ this-> month> = 12)
{
$ Lastmonth = $ this-> month-1;
$ Nextmonth = 1;
$ Lastyear = $ this-> year;
$ Nextyear = $ this-> year + 1;
}
 
$ Str = "<td colspan = '2'> <div align = 'center'> <a href = $ PHP_SELF? Year = ". $ lastyear." & month = ". $ lastmonth." & ><</a> </div> </td> ";
$ Str. = "<td colspan = '3'> <div align = 'center'> ". $ this-> year. "--". $ this-> month. "</div> </td> ";
$ Str. = "<td colspan = '2'> <div align = 'center'> <a href = $ PHP_SELF? Year = ". $ nextyear." & month = ". $ nextmonth." & >>></a> </div> </td> ";
Return $ str;
}
 
Function Show_Calendar ()
{
Echo "<table width = '000000' border = 5> <tr class = 'tr. title '> ". $ this-> title_link (). "</tr> <tr> ";
$ Weekarray = array ("day", "one", "two", "three", "four", "five", "Six ");

For ($ k = 0; $ k <= 6; $ k ++)
{
Echo "<td> <div align = 'center'>". $ weekarray [$ k]. "</div> </td> ";
}
Echo "</tr> ";

For ($ I = 0; $ I <= 5; $ I ++)
{
Echo "<tr> ";
For ($ j = 1; $ j <= 7; $ j ++)
{
$ Math = ($ j-$ this-> oneday_week () + 7 * $ I;

Echo "<td> <div align = 'center'> ";

If ($ math <= $ this-> endday () and $ math> = 1)
{
Echo $ math;
}

Echo "</div> </td> ";
}
Echo "</tr> ";
}
Echo "</table> ";
}
}
$ Calendar = new Calendar ($ _ GET ['Year'], $ _ GET ['month']);
$ Calendar-> month = $ _ GET ['month'];
$ Calendar-> year = $ _ GET ['Year'];
$ Calendar-> Show_Calendar ();
?>
Example 2: the code is as follows:
Copy codeThe Code is as follows: <? Php
Header ("content-type: text/html; charset = UTF-8 ");
?>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
 
<Link href = "statics/css/zhaoshang.css" rel = "stylesheet" type = "text/css">
<Div class = "rili">
<? Php
If (! Emptyempty ($ _ GET )){
$ Year = isset ($ _ GET ['Year'])? $ _ GET ['Year']: date ('y ');
$ Month = isset ($ _ GET ['month'])? $ _ GET ['month']: date ('M ');
$ Day = isset ($ _ GET ['day'])? $ _ GET ['day']: date ('D ');
}
If (emptyempty ($ year )){
$ Year = date ('y ');
}
If (emptyempty ($ month )){
$ Month = date ('M ');
}
 
If (emptyempty ($ month )){
$ Days = date ('D ');
}
 
$ Start_weekday = date ('w', mktime (0, 0, 0, $ month, 1, $ year ));
// Echo $ start_weekday;
$ Days = date ('T', mktime (0, 0, 0, $ month, 1, $ year ));
// Echo $ days;
$ Week = array ('day', 'yi', '2', '3', '4', '5', '6 ');
$ I = 0;
$ K = 1;
$ J = 0;
Echo '<table> ';
Echo '<tr> <td colspan = 7 style = "text-align: center; width: 250px; background: # d5e5f6; border-bottom: 1px solid #333366; "> '. $ year. 'Year '. $ month. 'month '. '</td> </tr> ';
Echo '<tr> ';
For ($ I = 0; $ I <7; $ I ++ ){
Echo '<td>'. $ week [$ I]. '</td> ';
}
Echo '</tr> ';
Echo '<tr> ';
For ($ j = 0; $ j <$ start_weekday; $ j ++ ){
Echo '<td style = "color: # f1f8fc;">'. $ j. '</td> ';
}
While ($ k <= $ days ){
If ($ k = $ day ){
Echo '<td style = "color: red"> <a href = "? Year = '. $ year. '& month = '. $ month. '& day = '. $ k. '"> <span style =" color: red "> '. $ k. '</span> </a> </td> ';
} Else {
Echo '<td> <a href = "? Year = '. $ year. '& month = '. $ month. '& day = '. $ k. '"> '. $ k. '</a> </td> ';
}
If ($ j + 1) % 7 = 0 ){
Echo '</tr> <tr> ';
}
$ J ++;
$ K ++;
}
While ($ j % 7! = 0 ){
Echo '<td style = "color: # f1f8fc;">'. $ j. '</td> ';
$ J ++;
}
Echo '</tr> ';
 
Echo '<tr style = "background: # d5e5f6;"> ';
Echo "<td> <a href =? ". LastYear ($ year, $ month)."> ". '<'. '</a> </td> ';
Echo "<td> <a href =? ". LastMonth ($ year, $ month)."> ". '<'. '</a> </td> ';
Echo '<td colspan = 3 style = "width: 105px;"> ';
Echo '<form name = "myform" method = "GET"> ';
Echo '<select name = year class = "year"> ';
For ($ start_year = 1970; $ start_year <2039; $ start_year ++ ){
$ Selected = '';
If ($ year = $ start_year) {$ selected = 'selected ';}
Echo '<option value ='. $ start_year. ''. $ selected. '>'. $ start_year. '</option>'." nt ";
}
Echo '</select> ';
Echo '<select name = month class = "month"> ';
For ($ start_month = 1; $ start_month <= 12; $ start_month ++ ){
$ Selected = '';
If ($ month = $ start_month) {$ selected = 'selected ';}
Echo '<option value ='. $ start_month. ''. $ selected. '>'. $ start_month. '</option>'." nt ";
}
Echo '</select> ';
/* Echo 'month ';*/
/* Echo '<input type = "submit" name = "search" value = "query"> ';*/
Echo '</form> ';
Echo '</td> ';
Echo "<td> <a href =? ". NextYear ($ year, $ month)."> ". '>'. '</a> </td> ';
Echo "<td> <a href =? ". NextMonth ($ year, $ month)."> ". '>'. '</a> </td> ';
Echo '</tr> ';
Echo '</table> ';
 
Function lastYear ($ year, $ month ){
$ Year = $ year-1;
Return "year = $ year & month = $ month ";
}
Function lastMonth ($ year, $ month ){
If ($ month = 1 ){
$ Year = $ year-1;
$ Month = 12;
} Else {
$ Month --;
}
Return "year = $ year & month = $ month ";
}
Function nextYear ($ year, $ month ){
$ Year = $ year + 1;
Return "year = $ year & month = $ month ";
}
Function nextMonth ($ year, $ month ){
If ($ month = 12 ){
$ Year = $ year + 1;
$ Month = 1;
} Else {
$ Month ++;
}
Return "year = $ year & month = $ month ";
}
 
?>
</Div>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.