CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Event API snippet: Am I an idiot or is this not working properly?
Pages: [1]

Author Topic: Event API snippet: Am I an idiot or is this not working properly?  (Read 1672 times)

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Event API snippet: Am I an idiot or is this not working properly?
July 16, 2008, 08:32:19 am
Hi,

I am sitting here double, triple, quadruple checking myself feeling like I am having a major brain glitch on the most basic of php syntax this morning. This code (taken from API snippets section on wiki) works perfectly to show all events where NOW is greater than the event start date:

Code: [Select]
<?php

function cma_date_new($a,$b) {
  if (
$a['start_date'] > $b['start_date']) return 1;
  if (
$a['start_date'] < $b['start_date']) return -1;
  return 
0;
}

if (
module_exists('civicrm')) {
  
civicrm_initialize(TRUE);
  require_once 
'api/v2/Event.php';
  
// This example limits listing to public and active events
  
$params = array ('is_public'     => 1, 'is_active'     => 1);
  
$myEvents = civicrm_event_search( $params );
  if (
$myEvents) {
    
$count = 0;
    
usort($myEvents,'cma_date_new');
    foreach (
$myEvents as $event) {
      
$now = date('Y-m-d H:i:s');
      if (
$now > $event['start_date']) continue;
      
$startdate = date('n/j',strtotime($event['start_date']));
      
$enddate = date('n/j',strtotime($event['end_date']));
      
      
// if it's a date range, we need to show both start/end dates; otherwise just show start
      
if($startdate != $enddate){
      
$finaldate = $startdate." - ".$enddate;
      }
      else{
      
$finaldate = $startdate;
      }
      
      
$eventid = $event['id'];
      list(
$title_place, $title_desc) = split(":",$event['title'],2);
    
      
$display = '<ul><li class="event"><span class="time">'.$finaldate.'</span>';
      
      
$display .= l($title_place.' '.$title_desc, 'civicrm/event/info', array(), 'reset=1&id='.$event['id']).'</li></ul>';
      echo 
$display;
      
$count++;
      if (
$count > 6) break;
    }
    if (
$count = 0) {
      echo 
'No events are currently scheduled.';
    }
  } else {
    echo 
'No events found.';
  }
}
?>

However, due to events with ranges, I actually need to show all events where NOW is less than the END date. But when I change:

Code: [Select]
      if ($now > $event['start_date']) continue;

to

Code: [Select]
if ($now < $event['end_date']) continue;
I get completely wrong results. It will only show events whose end date is way passed. I have looked at this like 300 times, and now I am at that point that's like you say a word so many times it doesn't look or sound right.

I have attached 3 screen shots for reference. The first is what displays with the original code, which is correct. The second is what displays if I change that one line. And in the third I have printed out NOW and then directly below it printed out the event's end date just to fully conclude that NOW is in fact GREATER than all of these end dates, and thus it shouldn't be displaying them. Can anyone tell me what's going on here? Am I just losing it today? I can't help but feel there is a stupid and simple explanation for my fault, but I just can't figure it out!



Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Event API snippet: Am I an idiot or is this not working properly?
July 16, 2008, 02:40:51 pm

I think your issue is you are comparing dates using string comparison functions and that probably does not do the right thing.you might want to convert your dates (check CRM/Utils/Date.php for some helper fucntions) and then use those functions

here is some code that we've used in CiviCRM

Code: [Select]
            $now = time( );

            $startDate = CRM_Utils_Date::unixTime( $this->_values['event']['registration_start_date'] );
            if ( $startDate &&
                 $startDate >= $now ) {
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Event API snippet: Am I an idiot or is this not working properly?
July 23, 2008, 07:52:57 am
Thanks, lobo. Some reformatting on your example and all is well again with the world. I updated the wiki page (http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets#UsingCiviCRMAPIs-CodeSnippets-ListPublicEventsbyEventTypeinaBlock) and here's my final code:

Code: [Select]
<?php

// We use this function to sort the events in descending order by start date; you can reverse the -1, 1 if you want it to sort in ascending order
function civi_date_sorting($a,$b) {
  if (
$a['start_date'] > $b['start_date']) return -1;
  if (
$a['start_date'] < $b['start_date']) return 1;
  return 
0;
}


if (
module_exists('civicrm')) {
  
civicrm_initialize(TRUE);
  require_once 
'api/v2/Event.php';
  
  
// This example limits listing to public and active events
  
$params = array ('is_public'     => 1, 'is_active'     => 1);
  
$myEvents = civicrm_event_search( $params );
  
  if (
$myEvents) {
    
$count = 0;
    
    
// Call the sorting function
    
usort($myEvents,'civi_date_sorting');
    foreach (
$myEvents as $event) {
      
$now = time( );
      
      
// Need to format the event end date in unix time, so we call a CiviCRM helper function
      
$endDate = CRM_Utils_Date::unixTime( $event['end_date'] );  
      
      
// We want to show any events whose end dates have not passed in case we have events that include ranges
      
if ( $now <= $endDate) { 
        
        
// Now we format the event start and end dates again to be user-friendly, we are using month/day format here
        
$startdate = date('n/j',strtotime($event['start_date']));
        
$enddate = date('n/j',strtotime($event['end_date']));
      
        
// if it's a date range, we need to show both start/end dates (7/1 - 7/10 for example); otherwise just show start (7/1)
        
if($startdate != $enddate){
          
$finaldate = $startdate." - ".$enddate;
        }
        else{
          
$finaldate = $startdate;
        }
      
        
$eventid = $event['id'];
        list(
$title_place, $title_desc) = split(":",$event['title'],2);
        
        
// We first display the dates, then add the title of the event and path to the registration page
        
$display = '<ul><li class="event"><span class="time">'.$finaldate.'</span>';
      
        
$display .= l($title_place.' '.$title_desc, 'civicrm/event/info', array(), 'reset=1&id='.$event['id']).'</li></ul>';
        echo 
$display;
        
        
// Increase the counter so we can know when to stop
        
$count++;
        
        
// Once we reach 6 events, stop
        
if ($count > 6) break;
      } 
// End if $now <= $endDate 
    
} // End foreach
    
if ($count = 0) {
      echo 
'No events are currently scheduled.';
    }
  } 
  else {
    echo 
'No events found.';
  }
} 
// End if CiviCRM exists

?>

greg72

  • Guest
Re: Event API snippet: Am I an idiot or is this not working properly?
July 31, 2008, 10:57:47 am
I used this code and for some reason it doesn't insert a space between the date and the event title. I can't seem to figure out how to get that space in there.

Code: [Select]
<?php

// We use this function to sort the events in descending order by start date
// You can reverse the -1, 1 if you want it to sort in ascending order
function civi_date_sorting($a,$b) {
  if (
$a['start_date'] > $b['start_date']) return 1;
  if (
$a['start_date'] < $b['start_date']) return -1;
  return 
0;
}


if (
module_exists('civicrm')) {
  
civicrm_initialize(TRUE);
  require_once 
'api/v2/Event.php';

  
// This example limits listing to public and active events
  
$params = array ('is_public'     => 0, 'is_active'     => 1);
  
$myEvents = civicrm_event_search( $params );

  if (
$myEvents) {
    
$count = 0;

    
// Call the sorting function
    
usort($myEvents,'civi_date_sorting');
    foreach (
$myEvents as $event) {
      
$now = time( );

      
// Need to format the event end date in unix time, so we call a CiviCRM helper function
      
$endDate = CRM_Utils_Date::unixTime( $event['end_date'] );

      
// We want to show any events whose end dates have not passed in case we have events that include ranges
      
if ( $now <= $endDate) {

        
// Now we format the event start and end dates again to be user-friendly, we are using month/day format here
        
$startdate = date('n/j',strtotime($event['start_date']));
        
$enddate = date('n/j',strtotime($event['end_date']));

        
// if it's a date range, we need to show both start/end dates (7/1 - 7/10 for example); otherwise just show start (7/1)
        
if($startdate != $enddate){
          
$finaldate = $startdate." - ".$enddate;
        }
        else{
          
$finaldate = $startdate;
        }

        
$eventid = $event['id'];
        list(
$title_place, $title_desc) = split(":",$event['title'],2);

        
// We first display the dates, then add the title of the event and path to the registration page
        
$display = '<ul><li class="event"><span class="time">'.$finaldate.'</span>';

        
$display .= l($title_place.' '.$title_desc, 'civicrm/event/info', array(), 'reset=1&id='.$event['id']).'</li></ul>';
        echo 
$display;

        
// Increase the counter so we can know when to stop
        
$count++;

        
// Once we reach 6 events, stop
        
if ($count > 6) break;
      } 
// End if $now <= $endDate
    
} // End foreach
    
if ($count = 0) {
      echo 
'No events are currently scheduled.';
    }
  }
  else {
    echo 
'No events found.';
  }
} 
// End if CiviCRM exists

?>

emilyf

  • Ask me questions
  • ****
  • Posts: 696
  • Karma: 54
  • CiviCRM version: 2.x - 4.x
  • CMS version: Drupal 5, 6, 7
Re: Event API snippet: Am I an idiot or is this not working properly?
July 31, 2008, 11:16:19 am
Try just inserting a space before the
Code: [Select]
</span> tag on this line (change to below):

Code: [Select]
$display = '<ul><li class="event"><span class="time">'.$finaldate.' </span>';

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Event API snippet: Am I an idiot or is this not working properly?

This forum was archived on 2017-11-26.