Php calender (calendar) Two version code examples

Source: Internet
Author: User

Php calender (calendar) Two version code examples

A simple php Calender (calendar) solves the 2038 problem, which can be used on both 32-bit and 64-bit machines. The code is very simple and easy to modify.

 

Note that the 32-bit server has 2038 problems, so the 32-bit server's validity period ranges from ~ 2038

 

We can also use DateTime to avoid this problem (this is irrelevant to 32-bit 64-bit)

 

The Code is as follows:

<? Php

/**

*

* My Calendar

* Date_default_timezone_set date mktime

* @ Param int $ year

* @ Param int $ month

* @ Param string $ timezone

* @ Author fc_lamp

* @ Blog: fc-lamp.blog.163.com

*/

Function myCalender ($ year = '', $ month ='', $ timezone = 'Asia/Shanghai ')

{

 

Date_default_timezone_set ($ timezone );

$ Year = abs (intval ($ year ));

$ Month = abs (intval ($ month ));

 

// Whether it is a 32-bit Server

If (is32 ())

{

If ($ year <1970 or $ year> = 2038)

{

$ Year = date ('y ');

}

} Else

{

If ($ year <= 0)

{

$ Year = date ('y ');

}

 

}

 

If ($ month <= 0 or $ month> 12)

{

$ Month = date ('M ');

}

 

// Previous year

$ PretYear = $ year-1;

// Last month

$ MpYear = $ year;

$ PreMonth = $ month-1;

If ($ preMonth <= 0)

{

$ PreMonth = 1;

$ MpYear = $ pretYear;

}

 

// Next year

$ NextYear = $ year + 1;

// Next month

$ MnYear = $ year;

$ NextMonth = $ month + 1;

If ($ nextMonth> 12)

{

$ NextMonth = 1;

$ MnYear = $ nextYear;

}

 

// Calendar Header

$ Html = <HTML

<Table width = "500" border = "1">

<Tr align = "center">

<Td> <a href = "? Y = $ pretYear "> previous year </a> </td>

<Td> <a href = "? Y = $ mpYear & m = $ preMonth "> last month </a> </td>

<Td> <a href = "? "> Back to today </a> </td>

<Td> <a href = "? Y = $ mnYear & m = $ nextMonth "> next month </a> </td>

<Td> <a href = "? Y = $ nextYear "> next year </a> </td>

</Tr>

<Tr align = "center">

<Td colspan = "5"> {$ year} year {$ month} month </td>

</Tr>

<Tr>

<Td colspan = "5">

<Table width = "100%" border = "1">

<Tr align = "center">

<Td style = "background-color: # DAF0DD;"> Monday </td>

<Td style = "background-color: # DAF0DD;"> Tuesday </td>

<Td style = "background-color: # DAF0DD;"> Wednesday </td>

<Td style = "background-color: # DAF0DD;"> Thursday </td>

<Td style = "background-color: # DAF0DD;"> Friday </td>

<Td style = "background-color: # F60; color: # fff; font-weight: bold;"> Saturday </td>

<Td style = "background-color: # F60; color: # fff; font-weight: bold;"> Sunday </td>

</Tr>

HTML;

 

$ CurrentDay = date ('Y-m-J ');

 

// Last day of the month

$ Lastday = date ('J', mktime (0, 0, 0, $ nextMonth, 0, $ year ));

 

// Number of days of cyclic output

$ Day = 1;

$ Line = '';

While ($ day <= $ lastday)

{

$ Cday = $ year. '-'. $ month. '-'. $ day;

 

// Current day of the week

$ NowWeek = date ('n', mktime (0, 0, 0, $ month, $ day, $ year ));

 

If ($ day = 1)

{

$ Line = '<tr align = "center"> ';

$ Line. = str_repeat ('<td> </td>', $ nowWeek-1 );

}

 

If ($ cday = $ currentDay)

{

$ Style = 'style = "color: red ;"';

} Else

{

$ Style = '';

}

 

$ Line. = "<td $ style> $ day </td> ";

 

// End of one week

If ($ nowWeek = 7)

{

$ Line. = '</tr> ';

$ Html. = $ line;

$ Line = '<tr align = "center"> ';

}

 

// End of the whole month

If ($ day = $ lastday)

{

If ($ nowWeek! = 7)

{

$ Line. = str_repeat ('<td> </td>', 7-$ nowWeek );

}

$ Line. = '</tr> ';

$ Html. = $ line;

 

Break;

}

 

$ Day ++;

}

 

$ Html. = <HTML

</Table>

</Td>

</Tr>

</Table>

HTML;

Return $ html;

}

 

/**

*

* Check whether it is a 32-bit server.

* @ Author fc_lamp

* @ Blog: fc-lamp.blog.163.com

*/

Function is32 ()

{

$ Is32 = False;

If (strtotime ('2017-10-10 ') = False)

{

$ Is32 = True;

}

Return $ is32;

}

 

 

 

Use the DateTime class to solve the 2038 problem. The Code is as follows:

 

The Code is as follows:

<? Php

/**

*

* My Calendar (DateTime Version)

* Date_default_timezone_set date mktime

* @ Param int $ year

* @ Param int $ month

* @ Param string $ timezone

* @ Author fc_lamp

* @ Blog: fc-lamp.blog.163.com

*/

Function myCalender ($ year = '', $ month ='', $ timezone = 'Asia/Shanghai ')

{

 

Date_default_timezone_set ($ timezone );

$ Year = abs (intval ($ year ));

$ Month = abs (intval ($ month ));

 

$ NowDate = new DateTime ();

 

If ($ year <= 0)

{

$ Year = $ nowDate-> format ('y ');

}

 

If ($ month <= 0 or $ month> 12)

{

$ Month = $ nowDate-> format ('M ');

}

 

// Previous year

$ PretYear = $ year-1;

// Last month

$ MpYear = $ year;

$ PreMonth = $ month-1;

If ($ preMonth <= 0)

{

$ PreMonth = 1;

$ MpYear = $ pretYear;

}

 

// Next year

$ NextYear = $ year + 1;

// Next month

$ MnYear = $ year;

$ NextMonth = $ month + 1;

If ($ nextMonth> 12)

{

$ NextMonth = 1;

$ MnYear = $ nextYear;

}

 

// Calendar Header

$ Html = <HTML

<Table width = "500" border = "1">

<Tr align = "center">

<Td> <a href = "? Y = $ pretYear "> previous year </a> </td>

<Td> <a href = "? Y = $ mpYear & m = $ preMonth "> last month </a> </td>

<Td> <a href = "? "> Back to today </a> </td>

<Td> <a href = "? Y = $ mnYear & m = $ nextMonth "> next month </a> </td>

<Td> <a href = "? Y = $ nextYear "> next year </a> </td>

</Tr>

<Tr align = "center">

<Td colspan = "5"> {$ year} year {$ month} month </td>

</Tr>

<Tr>

<Td colspan = "5">

<Table width = "100%" border = "1">

<Tr align = "center">

<Td style = "background-color: # DAF0DD;"> Monday </td>

<Td style = "background-color: # DAF0DD;"> Tuesday </td>

<Td style = "background-color: # DAF0DD;"> Wednesday </td>

<Td style = "background-color: # DAF0DD;"> Thursday </td>

<Td style = "background-color: # DAF0DD;"> Friday </td>

<Td style = "background-color: # F60; color: # fff; font-weight: bold;"> Saturday </td>

<Td style = "background-color: # F60; color: # fff; font-weight: bold;"> Sunday </td>

</Tr>

HTML;

 

$ CurrentDay = $ nowDate-> format ('Y-m-J ');

 

// Last day of the month

$ CreatDate = new DateTime ("$ year-$ nextMonth-0 ");

$ Lastday = $ creatDate-> format ('J ');

$ CreatDate = NULL;

 

// Number of days of cyclic output

$ Day = 1;

$ Line = '';

While ($ day <= $ lastday)

{

$ Cday = $ year. '-'. $ month. '-'. $ day;

 

// Current day of the week

$ CreatDate = new DateTime ("$ year-$ month-$ day ");

$ NowWeek = $ creatDate-> format ('n ');

$ CreatDate = NULL;

 

If ($ day = 1)

{

$ Line = '<tr align = "center"> ';

$ Line. = str_repeat ('<td> </td>', $ nowWeek-1 );

}

 

If ($ cday = $ currentDay)

{

$ Style = 'style = "color: red ;"';

} Else

{

$ Style = '';

}

 

$ Line. = "<td $ style> $ day </td> ";

 

// End of one week

If ($ nowWeek = 7)

{

$ Line. = '</tr> ';

$ Html. = $ line;

$ Line = '<tr align = "center"> ';

}

 

// End of the whole month

If ($ day = $ lastday)

{

If ($ nowWeek! = 7)

{

$ Line. = str_repeat ('<td> </td>', 7-$ nowWeek );

}

$ Line. = '</tr> ';

$ Html. = $ line;

 

Break;

}

 

$ Day ++;

}

 

$ Html. = <HTML

</Table>

</Td>

</Tr>

</Table>

HTML;

Return $ html;

}

 

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.