PHP Calender (Calendar) Two version code example (fix 2038 problem) _php Tutorial

Source: Internet
Author: User


Note 32-bit machines have 2038 issues, so the age range of 32-bit servers 1970 ~2038

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

Copy CodeThe code is as follows:
/**
*
* 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 = << <>















Last year Last month Back to today Next month Next year
{$year} year {$month} month




































































<>

HTML;$currentDay = Date (' Y-m-j ');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 = ' '; $line. = Str_repeat (' ', $nowWeek-1); } if ($cday = = $currentDay) {$style = ' style= ' color:red; ';} else {$style = ';} $line. = " "; End of Week if ($nowWeek = = 7) {$line. = ' ';$html. = $line;$line = ' '; }//All-month End if ($day = = $lastday) {if ($nowWeek! = 7) {$line. = Str_repeat (' ', 7-$nowWeek); } $line. = ' ';$html. = $line;Break}$day + +;}$html. = <<
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
$day


HTML;
return $html;
}

/**
*
* Check if it is a 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 solve the 2038 problem, so that there is no 32-bit and 64-bit, the code is as follows:

Copy CodeThe code is as follows:
/**
*
* 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 = << <>















Last year Last month Back to today Next month Next year
{$year} year {$month} month








































































<>

HTML;$currentDay = $nowDate->format (' y-m-j ');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 = ' '; $line. = Str_repeat (' ', $nowWeek-1); } if ($cday = = $currentDay) {$style = ' style= ' color:red; ';} else {$style = ';} $line. = " "; End of Week if ($nowWeek = = 7) {$line. = ' ';$html. = $line;$line = ' '; }//All-month End if ($day = = $lastday) {if ($nowWeek! = 7) {$line. = Str_repeat (' ', 7-$nowWeek); } $line. = ' ';$html. = $line;Break}$day + +;}$html. = <<
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
$day


HTML;
return $html;
}

http://www.bkjia.com/PHPjc/621670.html www.bkjia.com true http://www.bkjia.com/PHPjc/621670.html techarticle Note 32-bit machine has 2038 problems, so the 32-bit server's age range 1970 ~2038 We can also use DateTime to circumvent this problem (this is unrelated to the 32-bit 64-bit) copy code code such as ...

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