Solved

Output recurring dates

Posted on 2003-11-17
4
564 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

867 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

15 Experts available now in Live!

Get 1:1 Help Now