Advertisement
Shaun_B

Fixed date class (still WIP though) for blog site example

Oct 24th, 2012
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This will accept three parameters of type integer, representing
  4.  * a numerically written date, for instance, dd/mm/yy, assuming 2000 onwards.
  5.  * It will then construct and return a string based on the numeric
  6.  * date sent, so for instance 29/10/02 will return
  7.  * Tuesday 29th October 2002. It will also work with future dates, which is
  8.  * useful unless we start commonly using something different from the
  9.  * gregorian calendar.
  10.  * The bugs have been fixed when it was deployed on a live blog-site.
  11.  * Put in a folder called libraries and call it dateClass.php or something.
  12.  *
  13.  * @author:     Shaun_B
  14.  * @date:       2012-10-17
  15.  * @todo:       Accept four-digit length years (2003 rather than 03)
  16.  *          and also implement the 20th century bit, so that dates
  17.  *          between 1900 - 1999 are accepted too. Got caught up in
  18.  *          an OO frenzy (nearly).
  19.  *
  20.  **/
  21. class DateClass {
  22.     public $dayNo;
  23.     public $day;
  24.     public $month;
  25.     public $year;
  26.     public $fullDate;
  27.     public $ddmmyy;
  28.     public $dbDate;
  29.     public function __contruct() {
  30.         $this->dayNo = 0;
  31.         $this->day = 0;
  32.         $this->month = 0;
  33.         $this->year = 0;
  34.         $this->fullDate = '';
  35.         $this->ddmmyy = '';
  36.         $this->dbDate = '';
  37.     }
  38.     public function setDate( $d = null, $m = null, $y = 0) {
  39.         if (!$d || !$m ){
  40.             return 'Error in setDate';
  41.         } else {
  42.             $this->dayNo = $d;
  43.             $this->day = $this->dayFromDate( $d, $m, $y );
  44.             $this->month = $this->monthAsString( $m );
  45.             if ($y<=99 && $y>=0) {
  46.                 $this->year = $y+2000;
  47.             } else {
  48.                 $this->year = $y;
  49.             }
  50.             $this->fullDate = (string) $this->dateConstruct( $d, $m, $y );
  51.             if( $d <10 ) {
  52.                 $this->ddmmyy = '0' . $d;
  53.             } else {
  54.                 $this->ddmmyy = $d;
  55.             }
  56.             if( $m <10 ) {
  57.                 $this->ddmmyy .= '/0' . $m;
  58.             } else {
  59.                 $this->ddmmyy .= '/' . $m;
  60.             }
  61.             if( $y <10 ) {
  62.                 $this->ddmmyy .= '/0' . $y;
  63.             } else {
  64.                 $this->ddmmyy .= '/' . $y;
  65.             }
  66.             $this->dbDate = $this->year . '-';
  67.             if( $m<10 ) {
  68.                 $this->dbDate .= '0' . $m;
  69.             } else {
  70.                 $this->dbDate .= $m;
  71.             }
  72.             $this->dbDate .= '-';
  73.             if( $d<10 ) {
  74.                 $this->dbDate .= '0' . $d;
  75.             } else {
  76.                 $this->dbDate .= $d;
  77.             }
  78.         }
  79.     }
  80.     // Sets default year to zero (ie, 2000 etc...)
  81.     public static function dateConstruct( $dd = null, $mm = null, $yy = 0 ) {
  82.         // Number of days in each month (excluding leap-years)
  83.         // We have special conditions later for leap-years and February 29th:
  84.         $noOfDays = array( 1=>31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  85.         // This will test each variable sent to the function
  86.         // to see if it's not null and each is numeric:
  87.         if( !$dd || !$mm ) {
  88.             echo 'invalid or null date type, please enter numeric date as <br />';
  89.             echo 'DD/MM/YY, ie, 28/02/10.';
  90.             return;
  91.         }
  92.         // We'll transfer the sent variables to local variables:
  93.         $day = (int)$dd;
  94.         $month = (int)$mm;
  95.         $year = (int)$yy;
  96.         // We get onto the real testing now, to check that
  97.         // each numeric number entered is within the legal
  98.         // range (ie, DD/MM/YY):
  99.         if( $month<1 || $month>12 ) {
  100.             echo 'Month invalid, reset to default. <br />';
  101.             $month = 1;
  102.         }
  103.         if( $day<1 || $day>31 ) {
  104.             echo 'Day invalid, reset to default.<br />';
  105.             $day = 1;
  106.         }
  107.         if( $year<0 || $year>99 ) {
  108.             echo 'Year is invalid, reset to default.<br />';
  109.             $year = 1;
  110.         }
  111.         // Now further validation on the day to check for
  112.         // specific months and leap-years etc...
  113.         if( $day>29 && $month==2 ) {
  114.             echo 'Day invalid, reset to default.<br />';
  115.             $day = 1;
  116.         }
  117.         // Now let's do some calculations for the leap-year
  118.         // Remember that 2000 was a leap year and 1900 wasn't:
  119.         if( $day==29 && $month==2 && ((2000-$year)%4!=0) ) {
  120.             echo 'Not a leap year, reset to default.<br />';
  121.             $day = 28;
  122.         }
  123.         // Next, to validate the rest of the months:
  124.         if( $day>$noOfDays[ $month ] && $month!=2 ) {
  125.             echo 'Day not valid, reset to default.<br />';
  126.             $day = 1;
  127.         }
  128.         // Finally, let's print out the date as we need it:
  129.         if( $year<10 ) {
  130.             $returnValue = (string) DateClass::dayFromDate( $day, $month, $year )
  131.                 . ' ' . $day . DateClass::suffix( $day ) . ' ' . DateClass::monthAsString( $month ) . ' 200' . $year;
  132.         } else {
  133.             $returnValue = (string) DateClass::dayFromDate( $day, $month, $year )
  134.                 . ' '  . $day . DateClass::suffix( $day ) . ' ' . DateClass::monthAsString( $month ) . ' 20' . $year;
  135.         }
  136.         return $returnValue;
  137.     }
  138.     // Here are the functions to echo values as strings:
  139.     public static function monthAsString( $month = null ) {
  140.         $month = (int) $month;
  141.         // Here are the months of the year:
  142.         $monthToString = array( 1=>'January', 'February', 'March',
  143.                 'April', 'May', 'June', 'July', 'August',
  144.                 'September', 'October', 'Novermber', 'December' );
  145.         if( $month>0 && $month<13 ) {
  146.             return $monthToString[ $month ];
  147.         } else {
  148.             return 'Month not set';
  149.         }
  150.     }
  151.     // This will deal with the suffix of the numeric day,
  152.     // ie, 01/xx/xx will be 1st/xx/xx:
  153.     public static function suffix( $day = null ) {
  154.         switch( (int) $day ) {
  155.             case 1:
  156.                 return 'st';
  157.                 break;
  158.             case 2:
  159.                 return 'nd';
  160.                 break;
  161.             case 3:
  162.                 return 'rd';
  163.                 break;
  164.             case 21:
  165.                 return 'st';
  166.                 break;
  167.             case 22:
  168.                 return 'nd';
  169.                 break;
  170.             case 23:
  171.                 return 'rd';
  172.                 break;
  173.             case 31:
  174.                 return 'st';
  175.                 break;
  176.             default:
  177.                 return 'th';
  178.                 break;
  179.         }
  180.     }
  181.     public static function dayFromDate( $d = null, $m = null, $y = null ) {
  182.         $day = (int)$d;
  183.         $month = (int)$m;
  184.         $year = (int)$y;
  185.         // This is for the day validation. If the day is in the
  186.         // 20th century (1901 - 1999), here's the correct look-up
  187.         // table for the days according to my maths (has eight
  188.         // elements to stop errors):
  189.         $daysOfWeek20th = array( 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur', 'Sun', 'Mon' );
  190.         // Here is the same look-up table for the 21st century:
  191.         $daysOfWeek21st = array ( 'Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur', 'Sun', 'Mon' );
  192.         // Here is the month codes which will be used in the
  193.         // algorithm later on:
  194.         $monthCodes = array( 1=>6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4, 6 );
  195.         $a = $year+( abs( $year/4 ) );
  196.         $x = $monthCodes[ $month ];
  197.         $y = $day;
  198.         $z = $a + $x + $y;
  199.         $a = abs( $z );
  200.         $c = round( $a );
  201.         $v = $c % 7;
  202.         while( $v<=0 ) {
  203.             $v += 7;
  204.         }
  205.         $leapYear = (int)(2000-$year)%4;
  206.         // A bug in the algorithmn means that the year immediately proceeding
  207.         // a leap year is mis-calculated, so we need to increase $v by one:
  208.         if($leapYear==3) {
  209.             $v++;
  210.         }
  211.         // Another bug found: this one involves months after February on all
  212.         // years but leap-years.
  213.         if($month>=3 && $leapYear) {
  214.             $v--;
  215.         }
  216.         if( $month>=1  && $month<=2 ) {
  217.             return $daysOfWeek21st[ $v-1 ] . 'day';
  218.         } else if( $month>=3 ) {
  219.             return $daysOfWeek21st[ $v ] . 'day';
  220.         } else {
  221.             return 'Day unknown';
  222.         }
  223.     }
  224.     public function __destruct() {
  225.         ;;
  226.     }
  227. }
  228. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement