PHP Calender (Calendar) Two version code example (solve 2038 problem) _php instance

Source: Internet
Author: User
Tags abs



Note that 32-bit machine has 2038 problems, so 32-bit server life span 1970 ~2038 years

We can also use DateTime to circumvent this problem (this has nothing to do with 32-bit 64-bit)

Copy Code code 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 machine
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 ');
}

Last 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 = <<<table width= "border=" 1 ">
&LT;TR align= "center" >
<td><a href= "? y= $pretYear" > Last 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>
&LT;TR align= "center" >
&LT;TD colspan= "5" >{$year} year {$month} month </td>
</tr>
<tr>
&LT;TD colspan= "5" >
<table width= "100%" border= "1" >
&LT;TR align= "center" >
&LT;TD style= "Background-color: #DAF0DD;" > Monday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Tuesday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Wednesday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Thursday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Friday </td>
&LT;TD style= "Background-color: #F60; color: #fff; font-weight:bold;" > Saturday </td>
&LT;TD style= "Background-color: #F60; color: #fff; font-weight:bold;" > Sunday </td>
</tr>
HTML;

$currentDay = Date (' Y-m-j ');

The last day of the month
$lastday = Date (' J ', Mktime (0, 0, 0, $nextMonth, 0, $year));

Cycle Output Days
$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>";

The end of a week
if ($nowWeek = = 7)
{
$line. = ' </tr> ';
$html. = $line;
$line = ' <tr align= ' center ' > ';
}

All month End
if ($day = = $lastday)
{
if ($nowWeek!= 7)
{
$line. = Str_repeat (' <td> </td> ', 7-$nowWeek);
}
$line. = ' </tr> ';
$html. = $line;

Break
}

$day + +;
}

$html. = <<</table>
</td>
</tr>
</table>
HTML;
return $html;
}

/**
*
* Detect whether it is 32-bit machine
* @author Fc_lamp
* @blog: fc-lamp.blog.163.com
*/
function Is32 ()
{
$is = False;
if (Strtotime (' 2039-10-10 ') = = False)
{
$is = True;
}
return $is 32;
}

Use the DateTime class to troubleshoot 2038 problems, which are no more than 32 bits and 64 bits, as follows:

Copy Code code 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 ');
}

Last 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 = <<<table width= "border=" 1 ">
&LT;TR align= "center" >
<td><a href= "? y= $pretYear" > Last 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>
&LT;TR align= "center" >
&LT;TD colspan= "5" >{$year} year {$month} month </td>
</tr>
<tr>
&LT;TD colspan= "5" >
<table width= "100%" border= "1" >
&LT;TR align= "center" >
&LT;TD style= "Background-color: #DAF0DD;" > Monday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Tuesday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Wednesday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Thursday </td>
&LT;TD style= "Background-color: #DAF0DD;" > Friday </td>
&LT;TD style= "Background-color: #F60; color: #fff; font-weight:bold;" > Saturday </td>
&LT;TD style= "Background-color: #F60; color: #fff; font-weight:bold;" > Sunday </td>
</tr>
HTML;

$currentDay = $nowDate->format (' y-m-j ');

The last day of the month
$creatDate = new DateTime ("$year-$nextMonth-0");
$lastday = $creatDate->format (' J ');
$creatDate = NULL;

Cycle Output Days
$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>";

The end of a week
if ($nowWeek = = 7)
{
$line. = ' </tr> ';
$html. = $line;
$line = ' <tr align= ' center ' > ';
}

All month End
if ($day = = $lastday)
{
if ($nowWeek!= 7)
{
$line. = Str_repeat (' <td> </td> ', 7-$nowWeek);
}
$line. = ' </tr> ';
$html. = $line;

Break
}

$day + +;
}

$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.