Solved

Output recurring dates

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

734 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