?
Solved

php displaying dates

Posted on 2007-03-29
5
Medium Priority
?
234 Views
Last Modified: 2013-12-13
I'm developing a school website in php and would like to display a list of events occuring within the current actual month or the next month (so for example it is currently March, so all the events occuring in March and April). The page is at http://www.llantwitschool.org.uk/ and currently displays all the events in March. How can I now expand on this so that it displays all of the following months events too.

Here is the code:





<!-- change styles if you want -->
<style type="text/css">

A:link {text-decoration: none; color: black;}
A:visited {text-decoration: none; color: black;}
A:active {text-decoration: none; color: black;}
A:hover {text-decoration: underline; color: red;}
.textevent {font-weight: bold;font-family: verdana,arial; font-size: 13px; color: #000000; }
.textdate {font-family: verdana,arial; font-size: 9px; color: #000000; }
.htop {font-family: arial; font-size: 13px; color: #000000; }


</style>


</head>





<body>









<?php

$now = date("Y-m-d", time());
list($ty, $tm, $td) = explode('-',$now); // ty=thisyear, etc. used for highlighting 'today'



// THERE IS NO NEED TO EDIT BELOW HERE

$mon = date("m",mktime()); // month from clicked month name
$month = intval($mon); // var to use with month name array
if (strlen($mon)==1) { $mon = "0". $mon; }
$yr = date("Y",mktime()); // year from clicked month name



include("cal_parms.php");
include("./lang/dat_nam_en.php"); // load default language file so as not to mess anything upp with new text.
include($dat_names); // retrieved from cal_parms.php as a 'language' file
include("cal_db_conn.php");
mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!");
mysql_select_db($db_name) or die ("Can't open database!");

$min_dat = $yr. "-". $mon. "-01";
$max_dat = $yr. "-". $mon. "-31"; //sloppy but effective

// retrieve all events for the month

error_reporting(0);

$query = "SELECT * from $db_table WHERE date_format(ev_dat, '%Y-%m')='$yr-$mon' ORDER by ev_dat";
$result = @mysql_db_query($db_name, $query);

while ($myrow = mysql_fetch_array($result)) // loop through all results
{
    $found = $myrow['ev_dat'];
    $pieces = explode("-", $found);
    $monum = intval($pieces[1]);
    $danum = intval($pieces[2]);

    $yr = date("y",mktime());
    $mon = date("M",mktime());
   
    echo "[<span class='textdate'>". $danum. " ". $mon. " ". $yr. "</span>] ::";
    echo "<span class='textevent'>". "&nbsp;". $myrow['ev_title']. "</span><br />";


echo "<a href='javascript:winy(\"calendar/show-month.php?mon=$m&amp;yr=$y\")'>";
echo "<span style='text-decoration:none'>". $mo[intval($m)]. " ". $y. "</span></a>";




   
    $i++;
}


if ($i==0) { echo "<h1>".$noEventsText." ". $mo[$month]. "</h1>"; }
if ($sho == "1") {
    echo "";
}
else {
    echo "";
}
?>

</body>

</html>





Many thanks
0
Comment
Question by:llantwit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 18814332
I think something like this should work (haven't been able to test though)

<?php

$now = date("Y-m-d", time());
$cmonth = date('n', time());
$nextmonth = date("Y-m-d", mktime(1,1,1,$cmonth + 1,1));
$months = array($now, $nextmonth);

foreach ($months as $mnth) {
        list($ty, $tm, $td) = explode('-',$mnth); // ty=thisyear, etc. used for highlighting 'today'

        // THERE IS NO NEED TO EDIT BELOW HERE

        $mon = date("m",mktime()); // month from clicked month name
        $month = intval($mon); // var to use with month name array
        if (strlen($mon)==1) { $mon = "0". $mon; }
        $yr = date("Y",mktime()); // year from clicked month name

        include("cal_parms.php");
        include("./lang/dat_nam_en.php"); // load default language file so as not to mess anything upp with new text.
        include($dat_names); // retrieved from cal_parms.php as a 'language' file
        include("cal_db_conn.php");
        mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!");
        mysql_select_db($db_name) or die ("Can't open database!");

        $min_dat = $yr. "-". $mon. "-01";
        $max_dat = $yr. "-". $mon. "-31"; //sloppy but effective

        // retrieve all events for the month

        error_reporting(0);

        $query = "SELECT * from $db_table WHERE date_format(ev_dat, '%Y-%m')='$yr-$mon' ORDER by ev_dat";
        $result = @mysql_db_query($db_name, $query);

        while ($myrow = mysql_fetch_array($result)) { // loop through all results
                $found = $myrow['ev_dat'];
                $pieces = explode("-", $found);
                $monum = intval($pieces[1]);
                $danum = intval($pieces[2]);

                $yr = date("y",mktime());
                $mon = date("M",mktime());

                echo "[<span class='textdate'>". $danum. " ". $mon. " ". $yr. "</span>] ::";
                echo "<span class='textevent'>". "&nbsp;". $myrow['ev_title']. "</span><br />";


                echo "<a href='javascript:winy(\"calendar/show-month.php?mon=$m&amp;yr=$y\")'>";
                echo "<span style='text-decoration:none'>". $mo[intval($m)]. " ". $y. "</span></a>";

                $i++;
        }

        if ($i==0) { echo "<h1>".$noEventsText." ". $mo[$month]. "</h1>"; }
        if ($sho == "1") {
                echo "";
        }
        else {
                echo "";
        }
}
?>
0
 

Author Comment

by:llantwit
ID: 18814459
That code prints out all the events in March and then all the events in March after it (that is, prints them twice), rather than the events in April. Maybe you've gone through the loop twice but forgot to increment the month by 1.

Any ideas?
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 18814506
Okay, try this:

<?php

$now = date("Y-m-d", time());
$cmonth = date('n', time());
$nextmonth = mktime(1,1,1,$cmonth + 1,1);
$months = array(time(), $nextmonth);
list($ty, $tm, $td) = explode('-',$now); // ty=thisyear, etc. used for highlighting 'today'

foreach ($months as $mnth) {

        // THERE IS NO NEED TO EDIT BELOW HERE

        $mon = date("m",$mnth); // month from clicked month name
        $month = intval($mon); // var to use with month name array
        if (strlen($mon)==1) { $mon = "0". $mon; }
        $yr = date("Y",$mnth); // year from clicked month name

        include("cal_parms.php");
        include("./lang/dat_nam_en.php"); // load default language file so as not to mess anything upp with new text.
        include($dat_names); // retrieved from cal_parms.php as a 'language' file
        include("cal_db_conn.php");
        mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!");
        mysql_select_db($db_name) or die ("Can't open database!");

        $min_dat = $yr. "-". $mon. "-01";
        $max_dat = $yr. "-". $mon. "-31"; //sloppy but effective

        // retrieve all events for the month

        error_reporting(0);

        $query = "SELECT * from $db_table WHERE date_format(ev_dat, '%Y-%m')='$yr-$mon' ORDER by ev_dat";
        $result = @mysql_db_query($db_name, $query);

        while ($myrow = mysql_fetch_array($result)) { // loop through all results
                $found = $myrow['ev_dat'];
                $pieces = explode("-", $found);
                $monum = intval($pieces[1]);
                $danum = intval($pieces[2]);

                $yr = date("y",$mnth);
                $mon = date("M",$mnth);

                echo "[<span class='textdate'>". $danum. " ". $mon. " ". $yr. "</span>] ::";
                echo "<span class='textevent'>". "&nbsp;". $myrow['ev_title']. "</span><br />";


                echo "<a href='javascript:winy(\"calendar/show-month.php?mon=$m&amp;yr=$y\")'>";
                echo "<span style='text-decoration:none'>". $mo[intval($m)]. " ". $y. "</span></a>";

                $i++;
        }

        if ($i==0) { echo "<h1>".$noEventsText." ". $mo[$month]. "</h1>"; }
        if ($sho == "1") {
                echo "";
        }
        else {
                echo "";
        }
}
?>
0
 

Author Comment

by:llantwit
ID: 18814575
cheers that works well

one more thing - do you know how i can change the format of the days so that they have a leading zero if they are between 1 and 9 (e.g. 01 Mar 07)?
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18814705
Try changing this line:
$danum = intval($pieces[2]);

into

$danum = sprintf("%02d", intval($pieces[2]));
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question