I am writing an events program to help my dad manage his speaking schdule. The database setup is in the attached image.

I need to say something like this: take begin_date and end_date, if begin_date == end_date, echo $begin_date

else if begin_date(month) is not equal to end_date month, echo begin_date_month-begin_date_day - end_date_month-end_date_day, end_date_year (like this January 28-February 3, 2006)

else if begin_date_month is equal to end_date month, echo begin_date_month-begin_date_day - end_date_day, end_date_year (like this January 28-30, 2006)

Here's the code I currently use to get the events from the database.

$connection = @mysql_connect("localhost",$user,$password) or die("Could not connect to database...");
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM events WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) 
$result = @mysql_query($query,$connection)or die("failed on query: ". mysql_error());

while($row = mysql_fetch_array($result))

{

$venue = $row["venue"];

$event_name = $row["event_name"];

$begin_date = $row["begin_date"];
$end_date = $row["end_date"];
$location = $row["location"];
$commissioning = $row["commissioning"];

$contact_name = $row["contact_name"];

$contact_email = $row["contact_email"];





?>



<tr>

<td class="box"><hr><strong><? echo $begin_date ; ?></strong><br><br><? echo $venue; ?><br><? echo $location; ?><br><? echo $contact_name; ?><br><a href="mailto:<? echo $contact_email; ?>"><? echo $contact_email; ?></a><br><? if ($commissioning == 'yes')
{
    echo '<img src="images/commissioning.gif">';
}
else if ($commissioning == 'no')
{
    echo '';
} ?>

</tr>



<?

//end while

}



?>

Any help at all would be greatly apprecited. I've only been programming with php/mysql for about a month, so I'm still a n00b.

    while($row = mysql_fetch_array($result))
    {
      if(strtotime($row['begin_date']) == strtotime($row['end_date']))
      {
        echo $row['begin_date'];
      }
      elseif(date('M', strtotime($row['begin_date'])) != date('M', strtotime($row['end_date'])))
      {
        echo date('M d', strtotime($row['begin_date'])).'-'.date('M d, Y', strtotime($row['end_date'])));
      }
      elseif(date('M', strtotime($row['begin_date'])) == date('M', strtotime($row['end_date'])))
      {
        echo date('M d', strtotime($row['begin_date'])).'-'.date('d, Y', strtotime($row['end_date'])))
      }
    }

    Something like that?

      I'm trying to integrate your code into my page, but I'm not sure where it goes since I already have one while statement.

        Well, my while() statement was just to give you an anchor as to where the code would be inserted based upon your posted code. So where I have my while() statement is where your while() statement would go....

        Yours:

        $connection = @mysql_connect("localhost",$user,$password) or die("Could not connect to database...");
        mysql_connect(localhost,$user,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        $query="SELECT * FROM events WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) 
        $result = @mysql_query($query,$connection)or die("failed on query: ". mysql_error());
        
        while($row = mysql_fetch_array($result))
        {
                $venue = $row["venue"];
         	$event_name = $row["event_name"];
        	$begin_date = $row["begin_date"];
        	$end_date = $row["end_date"];
        	$location = $row["location"];
        	$commissioning = $row["commissioning"];
        	$contact_name = $row["contact_name"];
        	$contact_email = $row["contact_email"];
        ?>

        Mine:

        while($row = mysql_fetch_array($result))
        {
          if(strtotime($row['begin_date']) == strtotime($row['end_date']))
          {
            echo $row['begin_date'];
          }
          elseif(date('M', strtotime($row['begin_date'])) != date('M', strtotime($row['end_date'])))
          {
            echo date('M d', strtotime($row['begin_date'])).'-'.date('M d, Y', strtotime($row['end_date'])));
          }
          elseif(date('M', strtotime($row['begin_date'])) == date('M', strtotime($row['end_date'])))
          {
            echo date('M d', strtotime($row['begin_date'])).'-'.date('d, Y', strtotime($row['end_date'])))
          }
        } 

        So together:

        while($row = mysql_fetch_array($result))
        {
          /**
           *    $venue = $row["venue"];
           * 	$event_name = $row["event_name"];
           *	$begin_date = $row["begin_date"];
           *	$end_date = $row["end_date"];
           *	$location = $row["location"];
           *	$commissioning = $row["commissioning"];
           *	$contact_name = $row["contact_name"];
           *	$contact_email = $row["contact_email"];
           */
          if(strtotime($row['begin_date']) == strtotime($row['end_date']))
          {
            echo $row['begin_date'];
          }
          elseif(date('M', strtotime($row['begin_date'])) != date('M', strtotime($row['end_date'])))
          {
            echo date('M d', strtotime($row['begin_date'])).'-'.date('M d, Y', strtotime($row['end_date'])));
          }
          elseif(date('M', strtotime($row['begin_date'])) == date('M', strtotime($row['end_date'])))
          {
            echo date('M d', strtotime($row['begin_date'])).'-'.date('d, Y', strtotime($row['end_date'])))
          }
        } 

          Ok, I pasted your code exactly, and I get an error, but I'll work through it and figure it out. I will still need my existing while statement, since I am echoing those variables in my table. Thanks.

            Hmm... all date('M'... calls should be date('m'... note the case difference... sorry bout that....

              8 days later
              bpat1434 wrote:

              Hmm... all date('M'... calls should be date('m'... note the case difference... sorry bout that....

              I got it to work. Thanks for all of your help. Below is my code...

              <TABLE WIDTH=655 BORDER=0 CELLPADDING=0 CELLSPACING=0> 
               <?php 
              require("config.php");
              //create the connection to the database
              $connection = @mysql_connect("localhost",$user,$password) or die("Could not connect to database...");
              mysql_connect(localhost,$user,$password);
              //select the database
              @mysql_select_db($database) or die( "Unable to select database");
              // build query string
              $query="SELECT * FROM events ORDER BY begin_date";
              $result = @mysql_query($query,$connection)or die("failed on query: ". mysql_error());
              while($row = mysql_fetch_array($result))
              {
              
              $date = $row["date"];
              $venue = $row["venue"];
              $begin_date = $row["begin_date"];
              $end_date = $row["end_date"];
              $location = $row["location"];
              $contact_name = $row["contact_name"];
              $contact_email = $row["contact_email"];
              $commissioning = $row["commissioning"];
              ?>
              <tr>
              <td class="box"><hr>
              <strong><? if ($begin_date == $end_date) { echo date("F j, Y ", strtotime($begin_date)) ; }
              else if (date('m', strtotime($begin_date)) !=  date('m', strtotime($end_date)) )
              {    echo date("F j", strtotime($begin_date)). ' - ' .date("F j, Y", strtotime($end_date))   ; }
              else if (date('m', strtotime($begin_date)) ==  date('m', strtotime($end_date)) )
              {    echo date("F j", strtotime($begin_date)). '-' .date("j, Y", strtotime($end_date))   ; }
               ?></strong><br>
              <br><? echo $venue; ?>
              <br><? echo $event_name; ?>
              <br><? echo $location; ?>
              <br><? if ($commissioning == 'yes')
              {
                  echo '<img src="images/commissioning.gif">';
              }
              else if ($commissioning == 'no')
              {
                  echo '';
              } ?>
              <br><? echo $contact_name; ?>
              <br><a href="mailto:<? echo $contact_email; ?>"><? echo $contact_email; ?></a>
              
              </tr>
              <?
              //end while
              }
              ?>
              </table>
                Write a Reply...