Location: PHPKode > scripts > Mklib > mk_datetime.phps
<?

/*
    mk_datetime.php ver 0.1.0
    "putting the fun back in function"

    blazonry.com
    Created On: 2001-02-07


-----------------------------------------------------------------------
** CHANGES
-----------------------------------------------------------------------

    ver 0.1.0   2001-02-07
        * added mklib.php functions
        * added mk_splitDate()
    
    ver 0.0.5   
        * created drawCalendar function


        
*/

/*  ===================================================================
 *  Copyright (c) 1998-2000 Astonish Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  ====================================================================
 */


//----------------------------------------------------------------------------
//-- DATE AND TIME FUNCTIONS
//----------------------------------------------------------------------------

/** 
  * Process date in varying formats of mm-dd-yyyy or mm/dd/yyyy
  * 1 or 2 digit month or day and two or four digit year,
  * and returns yyyy-mm-dd  (four-digit year, two digit month,day)
  *
  * @param   date string
  * @return  formatted text or false on bad date
  */ 
function procDate($str) 
{
    /*== check for proper delimiter "-" or "/" ==*/
    if (strpos($str, "-")) 
        $token = "-";
    elseif (strpos($str, "/"))
        $token = "/";
    else { return false; }

    $m = strtok($str,$token);
    $d = strtok($token);
    $y = strtok($token);

    if (strlen($m) == 1) { $m = "0".$m; }
    if (strlen($d) == 1) { $d = "0".$d; }
    if (strlen($y) == 2) 
    {   
        /*== do some funky stuff for y2k ==*/
        if ($y > 70) { $y = "19".$y; }
        else { $y = "20".$y; }
    }

    return "$y-$m-$d";
}



function revDate($str) 
{
    // receives string as yyyy-mm-dd
    // returns mm-dd-yyyy

    $m = substr($str,5,2);
    $d = substr($str,8,2);
    $y = substr($str,0,4);

    return $m."-".$d."-".$y;
}


function chopSecs($str) 
{
    // removes seconds from HH:MM:SS
    return substr($str, 0,5);
}

    
function parseFullTimeStamp($str) 
{
    // parses MySQL Timestamp
    // returns mm-dd-yyyy HH:MM

    $y = substr($str, 0, 4);
    $m = substr($str, 4, 2);
    $d = substr($str, 6, 2);
    $hh = substr($str, 8,2);
    $mm = substr($str, 10,2);

    return $m."-".$d."-".$y." ".$hh.":".$mm;
}



function parseMySQLDate($str) 
{
    // parses MySQL Timestamp
    // returns date mm-dd-yyyy
    $y = substr($str, 0, 4);
    $m = substr($str, 4, 2);
    $d = substr($str, 6, 2);

    return $m."-".$d."-".$y;
}


/*== splits date into associate array ==*/
function mk_splitDate($str)
{
    $d["m"] = substr($str,5,2);
    $d["d"] = substr($str,8,2);
    $d["y"] = substr($str,0,4);

    return $d;
}



//*********************************************************
// DRAW CALENDAR
//*********************************************************
/*
    Draws out a calendar (in html) of the month/year
    passed to it date passed in format mm-dd-yyyy 
*/
function mk_drawCalendar($m,$y)
{
    if ((!$m) || (!$y))
    { 
        $m = date("m",mktime());
        $y = date("Y",mktime());
    }

    /*== get what weekday the first is on ==*/
    $tmpd = getdate(mktime(0,0,0,$m,1,$y));
    $month = $tmpd["month"]; 
    $firstwday= $tmpd["wday"];

    $lastday = mk_getLastDayofMonth($m,$y);

?>
<table cellpadding=4 cellspacing=0 border=1>
<tr><td colspan=7 bgcolor="#CCCCDD">
    <table cellpadding=0 cellspacing=0 border=0 width="100%">
    <tr><th width="20"><a href="<?=$SCRIPT_NAME?>?m=<?=(($m-1)<1) ? 12 : $m-1 ?>&y=<?=(($m-1)<1) ? $y-1 : $y ?>">&lt;&lt;</a></th>
    <th><?="$month $y"?></th>
    <th width="20"><a href="<?=$SCRIPT_NAME?>?m=<?=(($m+1)>12) ? 1 : $m+1 ?>&y=<?=(($m+1)>12) ? $y+1 : $y ?>">&gt;&gt;</a></th>
    </tr></table>
</td></tr>
<tr><th width=22 nowrap>Su</th><th width=22 nowrap>M</th>
    <th width=22 nowrap>T </th><th width=22 nowrap>W</th>
    <th width=22 nowrap>Th</th><th width=22 nowrap>F</th>
    <th width=22 nowrap>Sa</th></tr>
<?  $d = 1;
    $wday = $firstwday;
    $firstweek = true;

    /*== loop through all the days of the month ==*/
    while ( $d <= $lastday) 
    {

        /*== set up blank days for first week ==*/
        if ($firstweek) {
            print "<tr>";
            for ($i=1; $i<=$firstwday; $i++) 
            { print "<td><font size=2>&nbsp;</font></td>"; }
            $firstweek = false;
        }

        /*== Sunday start week with <tr> ==*/
        if ($wday==0) { print "<tr>"; }

        /*== print day cell ==*/
        /*== set color for today ==*/
        $cellclass = ($d == $today["mday"]) ? "todaycell" : "daycell";
        
        /*== check for event ==*/  
        $cellclass = ($d == 5) ? "eventcell" : "$cellclass";
        print "<td class='$cellclass'> $d</td>\n";

        /*== Saturday end week with </tr> ==*/
        if ($wday==6) { print "</tr>\n"; }

        $wday++;
        $wday = $wday % 7;
        $d++;
    }
?>
</tr></table>
<br>

<?
/*== end drawCalendar function ==*/
} 




/*== get the last day of the month ==*/
function mk_getLastDayofMonth($mon,$year)
{
    settype($mon, "integer");
    settype($year, "integer");
   
    $tday=29;
    while($tday <= 31) 
    {
        $tm = date("n", mktime(0,0,0,$mon,$tday,$year));
        if ($tm != $mon) { return $tday-1; }

        $tday++;
    }

    return 31;
}

?>
Return current item: Mklib