Home  >  Article  >  Backend Development  >  php calendar code efficient calendar code implemented in php

php calendar code efficient calendar code implemented in php

WBOY
WBOYOriginal
2016-07-25 08:56:052473browse
  1. /*

  2. * php calendar function
  3. * edit: bbs.it-home.org
  4. */
  5. @date_default_timezone_set('RPC');

  6. function show_calender(){

  7. $starttime=microtime();

  8. //Processing request date

  9. $y= isset($_GET["y"]) ? intval($ _GET["y"]) : date("Y");
  10. $m= isset($_GET["m"]) ? intval($_GET["m"]) : date("m");
  11. if ($m<1 || $m>12)$m=date("m");
  12. if($y<=0)$y=date("Y");

  13. $nm=$m+1;
  14. if($nm>12){
  15. $nm=1;
  16. $ny=$y+1;
  17. }else{
  18. $ny=$y;
  19. }

  20. //Previous month

  21. $pm=$m-1;
  22. if($pm<1){
  23. $pm=12;
  24. $py=$y-1;
  25. }else{
  26. $py=$y;
  27. }

  28. //Get the name of this month

  29. $month = date("M",mktime(0, 0, 0, $m, 1, $y));

  30. //Get the last day of this month

  31. $lastday = date("d",mktime(0, 0, 0, $nm, 0, $ny) );

  32. //Get the day of the week when the day starts

  33. $index = date("w",mktime(0, 0, 0, $m, 1, $y));< /p>
  34. //Generate calendar header

  35. echo "nn";

  36. //Processing calendar

  37. $row=0 ;
  38. $day=1;
  39. while(1){
  40. echo "
  41. ";
  42. if($row==0){
  43. for($i=0;$i<=6;$i++)
  44. if($i<$index)
  45. echo "
  46. ";
  47. else
  48. echo "
  49. ";
  50. }else{
  51. for($i=0;$i<=6;$i++)
  52. if($day>$lastday)
  53. echo "
  54. ";
  55. else
  56. echo "";
  57. }
  58. echo "
  59. ";
  60. $row++;
  61. if($day>$lastday)break;
  62. }
  63. echo "
  64. $endtime=microtime();

  65. $usedtime=$endtime-$starttime;
  66. return $usedtime;
  67. }

  68. $run_info= array();

  69. for($i=0;$i<10;$i++)
  70. $run_info[]=show_calender();

  71. print_r($run_info);

  72. ?>

  73. Copy code

    Test results:

    Array ( [0] => 0.00038400000000005 [1] => 0.00030999999999998 [2] => 0.000296999999999999 [3] => 0.000303 [4] => 0.00030399999999997 [5] => 0.00034499999999998 [6] => 0.00032300000000002 [7] => 0.00033 [8] => 0.00032699999999997 [9] => 0.00032000000000004 )

    >>>> Articles you may be interested in: php calender calendar code (solve 2038 problem) php calendar code (with demo effect) php calendar code sharing simple and practical php calendar code php calendar code Three good php calendar codes Share an example of PHP simple calendar code PHP simple calendar implementation code (can bind events)

  74. - {$y} {$month} +
    DayOneTwoThreeFour< ;/td>fivesix
      ".$day++."  


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn