PX : code

The datescroller by Bryan Hunt
Download this code


<?php

class dateJumperBase
{
    var 
$startTime 0;
    var 
$offsetStartTime 0;
    var 
$offsetEndTime 0;
    var 
$referenceOffset "month\\\";
    
    /**Constructor*/
    function dateJumperBase($default = 0)
    {
        if($default != 0)
        {
            $this->startTime =    $default;
        }
        else
        {
            $this->startTime = time();
        }
        echo \\\"<br> dateJumperBase \\\" . $default  . \\\" <br>\\\" ;
    }
    
    function getStartTime ()
    {
        return $this->startTime;    
    }
    
    function getOffsetStartTime ()
    {
            return $this->offsetStartTime;    
    }
    
    function getOffsetEndTime ()
    {
            return $this->offsetEndTime;    
    }
}

class dateJumperWithOffset extends dateJumperBase 
{
    /**Constructor*/
    function dateJumperWithOffset($default = 0,$offset = \\\"month\\\")
    {
        echo \\\"<br> dateJumperWithOffset \\\" . $default  . \\\" <br>\\\" ;
        /**Initialises base classes constructor*/
        $this->dateJumperBase($default);
        $this->referenceOffset = $offset;
        $this->setOffsetStartTime($offset);
        $this->setOffsetEndTime($offset);
    }

    function setOffsetStartTime ($offset = \\\"month\\\")
    {
        $currentDate = getdate ($this->startTime);
        switch($offset)
        {
            case \\\"day\\\":
                $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"], $currentDate[\\\"year\\\"]);
            break;
            case \\\"week\\\":
                $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , ($currentDate[\\\"mday\\\"] - ($currentDate[\\\"wday\\\"]) +1), $currentDate[\\\"year\\\"]);
            break;
            case \\\"month\\\":
                $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , 1, $currentDate[\\\"year\\\"]);
            break;
            case \\\"year\\\":
                $this->offsetStartTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"]);
            break;
            default:
            echo \\\"missing value in method setOffsetStartTime in class dateJumperWithOffset\\\";
        }
    }
    
    function setOffsetEndTime ($offset = \\\"month\\\")
    {
        $currentDate = getdate ($this->startTime);
        switch($offset)
        {
            case \\\"day\\\":
            $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + 1, $currentDate[\\\"year\\\"]);
            break;
            case \\\"week\\\":
            $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + ((7 - ($currentDate[\\\"wday\\\"]) )+1), $currentDate[\\\"year\\\"]);
            break;
            case \\\"month\\\":
            $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] +1 , 1, $currentDate[\\\"year\\\"]);
            break;
            case \\\"year\\\":
            $this->offsetEndTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"] + 1);
            break;
            default:
            echo \\\"missing value in method setOffsetStartTime in class dateJumperWithOffset\\\";
        }
    }
    
        function setJumpBackBy($offset = \\\"month\\\")
        {
            $currentDate = getdate ($this->offsetStartTime);
            switch($offset)
            {
                case \\\"day\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] -1, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"], $currentDate[\\\"year\\\"]);
                break;
                case \\\"week\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , ($currentDate[\\\"mday\\\"] - ($currentDate[\\\"wday\\\"]) +1) -7, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + ((7 - ($currentDate[\\\"wday\\\"]) )+1) -7, $currentDate[\\\"year\\\"]);
                break;
                case \\\"month\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] -1, 1, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"]  , 1, $currentDate[\\\"year\\\"]);                    
                break;
                case \\\"year\\\":
                    $this->offsetStartTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"] - 1);
                    $this->offsetEndTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"]);
                break;
                default:
                echo \\\"missing value in method setOffsetStartTime in class dateJumperWithOffset\\\";
            }
            $this->startTime = $this->offsetStartTime;
        }
        
        function setJumpForwardBy($offset = \\\"month\\\")
        {
            $currentDate = getdate ($this->offsetStartTime);
            switch($offset)
            {
                case \\\"day\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + 1, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + 2 , $currentDate[\\\"year\\\"]);
                break;
                case \\\"week\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , ($currentDate[\\\"mday\\\"] - ($currentDate[\\\"wday\\\"]) +1) +7, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] , $currentDate[\\\"mday\\\"] + ((7 - ($currentDate[\\\"wday\\\"]) )+1) +7, $currentDate[\\\"year\\\"]);
                break;
                case \\\"month\\\":
                    $this->offsetStartTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] +1, 1, $currentDate[\\\"year\\\"]);
                    $this->offsetEndTime = mktime (1,0,0, $currentDate[\\\"mon\\\"] +2 , 1, $currentDate[\\\"year\\\"]);
                break;
                case \\\"year\\\":
                    $this->offsetStartTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"] + 1);
                    $this->offsetEndTime = mktime (1,0,0, 1 , 1, $currentDate[\\\"year\\\"] + 2);
                break;
                default:
                echo \\\"missing value in method setOffsetStartTime in class dateJumperWithOffset\\\";
            }
            $this->startTime = $this->offsetStartTime;
        }
}

class dateJumperWithOffsetStringGen extends dateJumperWithOffset
{
    function dateJumperWithOffsetAndStringGeneration($default = 0,$offset = \\\"month\\\")
    {  
        echo $default;
        $this->dateJumperWithOffset($default,$offset);
    }

    /*
    This is a really important function, the name of the game here is rewriting the url 
    with the correct scroll_time, direction and jumpby values
    */
    function generateUrlString($scroll_time = 0 ,$direction = \\\"back\\\",$jumpby = \\\"months\\\")
    {
        global $PHP_SELF;
        global $QUERY_STRING;
        $alteredQuerySting = \\\"\\\";
        $QUERY_STRING = preg_replace (\\\"/scroll_time=[0-9]*/\\\",\\\"\\\", $QUERY_STRING,1);
        $QUERY_STRING = preg_replace (\\\"/direction=[forward|backward]*/\\\",\\\"\\\", $QUERY_STRING,1);
        $QUERY_STRING = preg_replace (\\\"/jumpby=[a-zA-Z]*/\\\",\\\"\\\", $QUERY_STRING,1);
        $QUERY_STRING = preg_replace (\\\"/&*$/\\\",\\\"\\\", $QUERY_STRING,1);
        $QUERY_STRING = preg_replace (\\\"/&{3,3}/\\\",\\\"\\\", $QUERY_STRING,1);
        $alteredQuerySting = $PHP_SELF.\\\"?scroll_time=\\\" . $scroll_time . \\\"&direction=\\\" . $direction . \\\"&jumpby=\\\". $jumpby . \\\"&\\\" . $QUERY_STRING ; 
        $alteredQuerySting = preg_replace (\\\"/&*$/\\\",\\\"\\\", $alteredQuerySting,1);
        return $alteredQuerySting;
    }
}



class dateJumperWithOffsetStringGenAndLastDayOfPeriod extends dateJumperWithOffsetStringGen
{
    function dateJumperWithOffsetStringGenAndLastDayOfPeriod ($default = 0,$offset = \\\"month\\\")
    {  
        echo $default;
        $this->dateJumperWithOffsetStringGen($default,$offset);
    }

    function getDaysInThisMonth()
    {
      return date(\"t\",$this->offsetStartTime);
    }
    
    function getOffset()
    {
        return $this->referenceOffset;
    }
}


?>

<?php
/*
If you want to test this class and see a reference implimentation 
just define a variable called testmode and watch it go 

*/

if(!empty($testmode))
{
        if(empty($scroll_time))
        {

        echo \\\"<br>scroll_time is empty<br>\\\";

        }

        empty($scroll_time) ? $scroll_time = 0 : 1 ;
        empty($jumpby) ? $jumpby = \\\"month\\\" : 1 ;

        echo \\\"<br>scroll_time is \\\" . $scroll_time . \\\"<br>\\\";

        if(!empty($scroll_time) && !empty($jumpby) )
        {
        $dateNew = new dateJumperWithOffsetStringGenAndLastDayOfPeriod ($scroll_time,$jumpby);
        }
        else
        {
        $dateNew = new dateJumperWithOffsetStringGenAndLastDayOfPeriod ();
        }

        if(!empty($direction))
        {
            if($direction == \\\"forward\\\")
            {
                $dateNew->setJumpForwardBy($jumpby);
            }
            if($direction == \\\"back\\\")
            {
                $dateNew->setJumpBackBy($jumpby);
            }
        }

        echo $dateNew->startTime . \\\"<br>\\\";
        echo \\\"<b>\\\" . date(\\\"D dS M Y @ g:i A \\\",$dateNew->startTime). \\\"<b>\\\" .\\\"<br>\\\";

        echo $dateNew->offsetStartTime . \\\"<br>\\\";
        echo \\\"<b>\\\" . date(\\\"D dS M Y @ g:i A \\\",$dateNew->getOffsetStartTime()). \\\"<b>\\\" . \\\"<br>\\\";

        echo $dateNew->offsetEndTime . \\\"<br>\\\";
        echo \\\"<b>\\\" . date(\\\"D dS M Y @ g:i A \\\",$dateNew->getOffsetEndTime()). \\\"<b>\\\" . \\\"<br>\\\";


        echo \\\"There are \\\" . $dateNew->getDaysInThisMonth(). \\\" Days in this month <br>\\\";




        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"back\\\",\\\"day\\\") . \\\"\\\\\\\">test back day</a><br>\\\";
        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"forward\\\",\\\"day\\\") . \\\"\\\\\\\">test forward day</a><br>\\\";

        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"back\\\",\\\"week\\\") . \\\"\\\\\\\">test back week</a><br>\\\";
        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"forward\\\",\\\"week\\\") . \\\"\\\\\\\">test forward week</a><br>\\\";

        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"back\\\",\\\"month\\\") . \\\"\\\\\\\">test back month</a><br>\\\";
        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"forward\\\",\\\"month\\\") . \\\"\\\\\\\">test forward month</a><br>\\\";

        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"back\\\",\\\"year\\\") . \\\"\\\\\\\">test back year</a><br>\\\";
        echo \\\"<a href=\\\\\\\"\\\" . $dateNew->generateUrlString($dateNew->getOffsetStartTime(),\\\"forward\\\",\\\"year\\\") . \\\"\\\\\\\">test forward year</a><br>\\\";
}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.