Output recurring dates

I have a MySQL table with events that occur weekly.  I would like to output in PHP a list of dates that a selected event occured (once a week) since a selected date.  For instance, I know that there is an event that occurs every Monday at 10:00AM.  I would like to give this function a starting date, say October 26th, 2003. I would then like to have a list similar to this:

November 17
November 10
November 03
October 27

Here is the event table structure:

INT - EventID
INT - EventDay (day of week event occurs, 1=Monday 7=Sunday)
INT - EventTime (hour that event starts, 14 = 2PM)
INT - ShowID - key to foreign table for data about event


vk33 posted this on the MySQL board that looks pretty promising in Java:
___________________________________________
// init database connection before...
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM events WHERE eventID = " + eventID);
int day = rs.getInt("eventDay");
Calendar start = new GregorianCalendar(2003,10,26);
int startDay = start.get(Calendar.DAY_OF_WEEK);
if (startDay != day)
   if (startDay > day)
      start.add(Calendar.DAY_OF_YEAR, 7-startDay + day);
   else
      start.add(Calendar.DAY_OF_YEAR, startDay-day);
Calendar current = new GregorianCalendar();
while (current.after(start)) {
   System.out.println (start.toString());
   start.add(Calendar.DAY_OF_YEAR,7);
}
_________________________________________

The problem is that I can't find any comparable functions in PHP.  What is the best way to do this in PHP?  For reference, here is the previous post in the MySQL forum:
http://www.experts-exchange.com/Databases/Mysql/Q_20800026.html

Thanks again!

-John Oleinik
joleinikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lozlozCommented:
function get_dates($event) {
  $query = "SELECT * FROM events WHERE eventID = '$event'";
  $result = mysql_query($query) or die(mysql_error() . $query);
  $numrows = mysql_num_rows($result);
  if($numrows > 0) {
    $row = mysql_fetch_assoc($result);
    $startingdate = mktime(0,0,0,10,26,2003);
    $startingday = date("w", $startingdate); # 0 = sun, 6 = say
    if($startingday == 0) {
      $startingday = 7;
    }
    if($startingday != $row["eventDay"]){
      if($startingday > $day) {
        $startingdate = 7 - $startingdate + ($day * 24 * 60 * 60);
      } else {
        $startingdate = $startingdate - ($day * 24 * 60 * 60);
      }
    }
    $current = time();
    while($current > $startingdate) {
      $datestring = date("F d", $startingdate);
      print $datestring;
      $datestring = $datestring + (7 * 24 * 60 * 60);
    }
  } else {
    print "No event found for that ID";
  }
}

get_dates($_POST["eventid"]);

i haven't tested that but tell me how it goes, i've pretty much copied the java code and converted it to php. if there are any errors then stick them up here and i'll have a go at fixing them

loz
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sairam_SCommented:
I hope this would solve the problem for any no of recurring days, hours,months and years too

<?php

      $currDt = date("Y-m-d", mktime(0, 0, 0, 10, 26, 2003)); //the start date
      $currTs = mktime(0, 0, 0, 10, 26, 2003);  // timestamp of the start date
      $incrementBy = 6; //difference between 2 days
      $noOfWeeks   = 5; //total no. of days to be generated
      echo $currDt;  //First date
      while($noOfWeeks >0)
      {
            $currDt = date("Y-m-d", strtotime(" +" . $incrementBy ." days", $currTs)); //generate the next day as firstdate + increment by
            $currTs = strtotime(" +" . $incrementBy ." days", $currTs); //do the sameto increment the timestamp
            echo "<br />Date is " . $currDt; //display the date.
            $noOfWeeks--;
      }


?>

I hope this is convincing...
0
shmertCommented:
Use this function to add 7 days to your starting date, as many times as you want:

/**
* Adds <code>$days</code>, <code>$months</code>, <code>$years</code> to <code>$start</code>.
* If <code>$start</code> is not provided, the current time is used.
* @author Sam Barnum
*/
function dateAdd($days, $months=0, $years=0, $start=null) {
        if (!$start) $start = time();
        $now = getDate($start);
        return mktime($now['hours'], $now['minutes'], $now['seconds'], $now['mon'] + $months, $now['mday'] + $days, $now['year'] + $years);
}

Example:
<?php
$query = 'select eventID, startDate FROM event';
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    $startDate = strtotime($row['startDate']);
    for ($week=0; $week<=5; $week++) {
        echo date('m/d/Y', $startDate);
        $startDate = dateAdd(7, 0, 0, $startDate);
    }
}
?>
0
joleinikAuthor Commented:
Thanks a bunch guys!  

-John Oleinik
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.