Advertisement
Shaun_B

Improved OOP date functions with MySQL-friendly generation.

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