Advertisement
Shaun_B

Returning the day on any particular date (21st Century)

Oct 10th, 2012
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.23 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.  *
  11.  * @author: Shaun_B
  12.  * @date:   2012-10-10
  13.  * @todo:   Accept four-digit length years (2003 rather than 03)
  14.  *      and also implement the 20th century bit, so that dates
  15.  *      between 1900 - 1999 are accepted too.
  16.  *
  17.  **/
  18. class DateClass {
  19.     // Sets default year to zero (ie, 2000 etc...)
  20.     public static function dateConstruct( $dd = null, $mm = null, $yy = 0 ) {
  21.     // Number of days in each month (excluding leap-years)
  22.     // We have special conditions later for leap-years and February 29th:
  23.     $noOfDays = array( 1=>31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  24.     // This will test each variable sent to the function
  25.     // to see if it's not null and each is numeric:
  26.     if( !$dd || !$mm ) {
  27.         echo 'invalid or null date type, please enter numeric date as <br />';
  28.         echo 'DD/MM/YY, ie, 28/02/10.';
  29.         return;
  30.     }
  31.     // We'll transfer the sent variables to local variables:
  32.     $day = $dd;
  33.     $month = $mm;
  34.     $year = $yy;
  35.     // We get onto the real testing now, to check that
  36.     // each numeric number entered is within the legal
  37.     // range (ie, DD/MM/YY):
  38.     if( $month<1 || $month>12 ) {
  39.         echo 'Month invalid, reset to default. <br />';
  40.         $month = 01;
  41.     }
  42.     if( $day<1 || $day>31 ) {
  43.         echo 'Day invalid, reset to default.<br />';
  44.         $day = 01;
  45.     }
  46.     if( $year<0 || $year>99 ) {
  47.         echo 'Year is invalid, reset to default.<br />';
  48.         $year = 01;
  49.     }
  50.     // Now further validation on the day to check for
  51.     // specific months and leap-years etc...
  52.     if( $day>29 && $month==2 ) {
  53.         echo 'Day invalid, reset to default.<br />';
  54.         $day = 01;
  55.     }
  56.     // Now let's do some calculations for the leap-year
  57.     // Remember that 2000 was a leap year and 1900 wasn't:
  58.     if( $day==29 && $month==2 && ((2000-$year)%4!=0) ) {
  59.         echo 'Not a leap year, reset to default.<br />';
  60.         $day = 28;
  61.     }
  62.     // Next, to validate the rest of the months:
  63.     if( $day>$noOfDays[ $month ] && $month!=2 ) {
  64.         echo 'Day not valid, reset to default.<br />';
  65.         $day = 01;
  66.     }
  67.     // Finally, let's print out the date as we need it:
  68.     if( $year<10 ) {
  69.             $returnValue = (string) DateClass::dayFromDate( $day, $month, $year )
  70.             . 'day ' . $day . DateClass::suffix( $day ) . ' '
  71.             . DateClass::monthAsString( $month ) . ' 200' . $year . '<br />';
  72.         } else {
  73.             $returnValue = (string) DateClass::dayFromDate( $day, $month, $year )
  74.             . 'day ' . $day . DateClass::suffix( $day ) . ' '
  75.             . DateClass::monthAsString( $month ) . ' 20' . $year . '<br />';
  76.         }
  77.         return $returnValue;
  78.     }
  79.     // Here are the functions to echo values as strings:
  80.     public static function monthAsString( $month = null ) {
  81.         // Here are the months of the year:
  82.         $monthToString = array( 1=>'January', 'February', 'March',
  83.                 'April', 'May', 'June', 'July', 'August',
  84.                 'September', 'October', 'Novermber', 'December' );
  85.         if( $month>0 && $month<13 ) {
  86.             return $monthToString[ $month ];
  87.         } else {
  88.             return 'Month not set';
  89.         }
  90.     }
  91.     // This will deal with the suffix of the numeric day,
  92.     // ie, 01/xx/xx will be 1st/xx/xx:
  93.     public static function suffix( $day = null ) {
  94.         switch( (int) $day ) {
  95.             case 1:
  96.                 return 'st';
  97.                 break;
  98.             case 2:
  99.                 return 'nd';
  100.                 break;
  101.             case 3:
  102.                 return 'rd';
  103.                 break;
  104.             case 21:
  105.                 return 'st';
  106.                 break;
  107.             case 22:
  108.                 return 'nd';
  109.                 break;
  110.             case 23:
  111.                 return 'rd';
  112.                 break;
  113.             case 31:
  114.                 return 'st';
  115.                 break;
  116.             default:
  117.                 return 'th';
  118.                 break;
  119.         }
  120.     }
  121.     public static function dayFromDate( $d = null, $m = null, $y = null ) {
  122.         $day = $d;
  123.         $month = $m;
  124.         $year = $y;
  125.         // This is for the day validation. If the day is in the
  126.         // 20th century (1901 - 1999), here's the correct look-up
  127.         // table for the days according to my maths (has eight
  128.         // elements to stop errors):
  129.         $daysOfWeek20th = array( 'Mon', 'Tues', 'Wednes',
  130.             'Thurs', 'Fri', 'Satur', 'Sun', 'Mon' );
  131.         // Here is the same look-up table for the 21st century:
  132.         $daysOfWeek21st = array ( 'Sun', 'Mon', 'Tues',
  133.             'Wednes', 'Thurs', 'Fri', 'Satur', 'Sun', 'Mon' );
  134.         // Here is the month codes which will be used in the
  135.         // algorithm later on:
  136.         $monthCodes = array( 1=>6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4, 6 );
  137.         $a = $year+( abs( $year/4 ) );
  138.         $x = $monthCodes[ $month ];
  139.         $y = $day;
  140.         $z = $a + $x + $y;
  141.         $a = abs( $z );
  142.         $c = round( $a );
  143.         $v = $c % 7;
  144.         while( $v<=0 ) {
  145.             $v += 7;
  146.         }
  147.         $leapYear = (2000-$year)%4;
  148.         // A bug in the algorithmn means that the year immediately proceeding
  149.         // a leap year is mis-calculated, so we need to increase $v by one:
  150.         if($leapYear==3)
  151.         {
  152.             $v++;
  153.         }
  154.         // Another bug found: this one involves months after February on all
  155.         // years but leap-years.
  156.         if($month>=3 && $leapYear)
  157.         {
  158.             $v--;
  159.         }
  160.         if( $month>=1  && $month<=2 ) {
  161.             return $daysOfWeek21st[ $v-1 ];
  162.         } else if( $month>=3 ) {
  163.             return $daysOfWeek21st[ $v ];
  164.         } else {
  165.             return 'Day unknown';
  166.         }
  167.     }
  168. }
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement