Solved

Output recurring dates

Posted on 2003-11-17
4
563 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:joleinik
4 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9763144
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
 
LVL 2

Expert Comment

by:Sairam_S
ID: 9763433
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
 
LVL 11

Expert Comment

by:shmert
ID: 9764711
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
 

Author Comment

by:joleinik
ID: 9768891
Thanks a bunch guys!  

-John Oleinik
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now