Jump to content

March 13th???


jrm

Recommended Posts

Does March 13 2011 not exist in the php Date object?  I have included my code for a calendar to display info based on a week Fri-Thur.  It does great except for Mar13.  Also Mar 14, 2010 seams to be missing as well.  What did I do wrong?

 

function viewAllMerits()
{        
        $cDates = count($this->Cadet["Dates"]);
        echo "\n<br>class Merits::viewAllMerits::Dates=";print_r($this->Cadet["Dates"]); 
        //echo "\n<br>class Merits::viewAllMerits:Merits=";print_r($this->Merits);
        for($k = 1; $k <= $cDates-2; $k++)
        {
            $t = $k; $p = $t + 1;
            $start = strtotime($this->Cadet["Dates"][$k]["DTG"]);
            $stop  = strtotime($this->Cadet["Dates"][$p]["DTG"]);
            $phase = $this->Cadet["Dates"][$k]["Type"];
            
                        		        
            $dayTotal = ($stop - $start)/60/60/24;
            $weekTotal = ceil($dayTotal/7)+1; //to correct not printing partial weeks
            
            echo "\n<table style=\"align:center;width:100%;\">";
            echo "\n    <tr>";
            echo "\n        <th colspan=\"7\" align=\"center\"><strong>";
            echo "\n            Merits earned during ". $phase ." from ". date("d M Y", $start) . " to " . date("d M Y",$stop-3600) . " ";
            echo "\n            <br />Days in phase:". floor($dayTotal);
            echo "\n            ";
            echo "\n        </strong></th>";
            echo "\n    </tr>";
            echo "\n    <tr>";
            echo "\n        <th>Fri</th>";
            echo "\n        <th>Sat</th>";
            echo "\n        <th>Sun</th>";
            echo "\n        <th>Mon</th>";
            echo "\n        <th>Tue</th>";
            echo "\n        <th>Wed</th>";
            echo "\n        <th>Thu</th>";
            echo "\n    </tr>";      
            
            //echo "\n<br>class Merits::viewAllMerits:weekTotal=$weekTotal";                        
            for($i = 0; $i < $weekTotal; $i++)
            {
                echo "\n    <tr>";
                for ($j = 1; $j <= 7; $j++)
                {                
                    $dayNum = ($j + $i*7 - date("w",$start))-2;
                    //Print a cell with the day number in it.  If it is today, highlight it.
                    if ($dayNum > 0 && $dayNum <= $dayTotal)
                    {
                        $temp = $start+($dayNum*24*60*60)-3600;
                        //$newdate = date("Y-m-d",$temp);
                        //$id = $this->multi_array_search($newdate, $this->Merits[$phase]);
                        //echo "\n<br>id=";print_r($id);
                        //$link = ($id == false ? "N/A": $this->Merits[$phase]["Detail"][$id[1]]["Total"]);
                        echo "\n        <td>";
                        echo "\n            <table>";
                        echo "\n                <tr>";
                        echo "\n                    <th colspan=\"1\">". date("d-M",$temp) ."</th>";
                        echo "\n                </tr>";
                        echo "\n                <tr>";
                        //need to develop pop up for details
                        //$test = "<a href=\"" . $_SERVER["PHP_SELF"] . "?outer=Cadets&id=" . $this->CadetID . "&inner=Merits\">$link</a>";
                        echo "\n                    <td>".$link."</td>";
                        echo "\n                </tr>";
                        echo "\n            </table>";
                        echo "\n        </td>";
                    }
                    else 
                    {
                        //Print a blank cell if no date falls on that day, but the row is unfinished.
                        echo "\n        <td></td>";
                    }
                }
                echo "\n    </tr>";
                if ($dayNum >= $dayTotal && $i != 6) break;
            }
            echo "\n</table>";
        } 
}

 

data:

class Merits::viewAllMerits::Dates=Array
(
    [0] => Array
        (
            [Type] => Date of Entry
            [DTG] => 2010-10-19
            [Comments] => 
        )

    [1] => Array
        (
            [Type] => Orientation
            [DTG] => 2010-10-19
            [Comments] => 
        )

    [2] => Array
        (
            [Type] => 1st Phase
            [DTG] => 2010-11-19
            [Comments] => 
        )

    [3] => Array
        (
            [Type] => 2nd Phase
            [DTG] => 2011-02-18
            [Comments] => 
        )

    [4] => Array
        (
            [Type] => 3rd Phase
            [DTG] => 2011-05-20
            [Comments] => PT PATCH
        )

    [5] => Array
        (
            [DTG] => 2011-07-19
        )

)

Link to comment
Share on other sites

To loop over a date range (valid from 1970 or 1901, depending on php version and op system, to 2038) -

<?php
$date = '2011-07-01'; // starting date (YYYY-MM-DD)
$end = '2011-07-31'; // ending date (YYYY-MM-DD)
while($date <= $end){
// your code using the $date
echo $date . '<br />';

$date = date('Y-m-d',strtotime("$date +1 day")); // get the next date
}
?>

Link to comment
Share on other sites

http://geography.about.com/cs/daylightsavings/a/dst.htm

 

The issue is that not all days have 24 hours in a timezone with daylight savings.  Some have 23 and some have 25.  So you can't rely on adding 24 hours to get to the next day.

 

Thanks btherl for the info on the dst.  After looking at it, I decided to look at past years, and the same thing happens every year that I have info for on those dst dates.

 

To loop over a date range (valid from 1970 or 1901, depending on php version and op system, to 2038) -

<?php
$date = '2011-07-01'; // starting date (YYYY-MM-DD)
$end = '2011-07-31'; // ending date (YYYY-MM-DD)
while($date <= $end){
// your code using the $date
echo $date . '<br />';

$date = date('Y-m-d',strtotime("$date +1 day")); // get the next date
}
?>

 

Thank you also PFMaBiSmAd for the loop.  Although I am having trouble making the format with it.  I am still working on it.

 

Link to comment
Share on other sites

Now, this works for me, as our week starts on Fri, not Sun, and takes in account of the day light savings time issues.

 

This takes an array of dates from a mysql query, and displays them in a calendar style view

$cDates = count($Dates);
        
for($k = 1; $k <= $cDates-2; $k++)
{
    $t = $k; $p = $t + 1;
    $start = strtotime($Dates[$k]["DTG"]);
    $stop  = strtotime($Dates[$p]["DTG"]);
    $dayTotal = ($stop - $start)/24/60/60;
            
    $date = date("Y-m-d", $start); // starting date (YYYY-MM-DD)
    $temp = date("Y-m-d", $stop); // ending date (YYYY-MM-DD)
    $end = date("Y-m-d",strtotime("-1 day",$stop));
         
    echo "\n<table style=\"align:center;width:100%;\">";
    echo "\n    <tr>";
    echo "\n        <th colspan=\"7\" align=\"center\"><strong>";
    echo "\n        </strong></th>";
    echo "\n    </tr>";
    echo "\n    <tr>";
    echo "\n        <th>Fri</th>";
    echo "\n        <th>Sat</th>";
    echo "\n        <th>Sun</th>";
    echo "\n        <th>Mon</th>";
    echo "\n        <th>Tue</th>";
    echo "\n        <th>Wed</th>";
    echo "\n        <th>Thu</th>";
    echo "\n    </tr>";      
    echo "\n    <tr>";

    $offset = date("w", strtotime($date));
    if($offset <= 4) // to add spaces to beginning of row
    {
        for($i = 0; $i <= $offset+1; $i++){echo "\n    <td><br /></td>";}
        $count = $offset+2;
    }
    else
    {
        $count = 0;
    }
         
    while($date <= $end)
    {
        echo "\n        <td>" . date("d M", strtotime($date)) ."::". date("w",strtotime($date)). "</td>";
         
        $date = date('Y-m-d',strtotime("$date +1 day")); // get the next date
        if($count >= 6) // to set next row
        {
            $count = 0;
            echo "\n    </tr><tr>";
        }
        else
        {
            $count++;
        }
    }
    for($i = $count; $i <= 6; $i++){echo "\n        <td><br /></td>";}// to add spaces at end of row
    echo "\n    </tr>"; 
    echo "\n</table>";
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.