diff($past);
$chosen_date = $today;
$days_range1 = $interval->days;
$days_range2 = 0;
// Do some ajax stuff
if (isset($_POST['range1'])) {
	$range1 = $_POST['range1'];
	$range2 = $_POST['range2'];
	
	$choice1 = new DateTime($range1);
	$interval = $today_date->diff($choice1);
	$days_range1 = $interval->days;
	$choice2 = new DateTime($range2);
	$interval = $today_date->diff($choice2);
	$days_range2 = $interval->days;
	$year_ago = $range1;
	$today = $range2;
}	
/*
SELECT contact_id, full_name, email, phone, sort_visits, sort_hours FROM 
(SELECT contacts.contact_id, CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name,
contacts.email AS email, contacts.phone AS phone, 
COUNT(shop_hours.contact_id) as sort_visits, 
ROUND(SUM(HOUR(SUBTIME( TIME(time_out), TIME(time_in))) + MINUTE(SUBTIME( TIME(time_out), TIME(time_in)))/60)) AS sort_hours 
FROM shop_hours 
LEFT JOIN contacts ON shop_hours.contact_id = contacts.contact_id 
LEFT JOIN shop_user_roles ON shop_hours.shop_user_role = shop_user_roles.shop_user_role_id 
WHERE (time_in > DATE_SUB(CURDATE(),INTERVAL 12 MONTH)) 
AND shop_user_roles.volunteer = 1 GROUP BY contact_id) AS members 
WHERE sort_hours >= 8 AND sort_visits >= 2 
GROUP by contact_id ORDER by sort_hours  DESC, sort_visits DESC;
SELECT contacts.contact_id, 
CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name, 
CONCAT(first_name, ' ', last_name) AS normal_full_name,
contacts.email AS email, contacts.phone AS phone, 
transaction_log.date as sort_hours 
FROM transaction_log 
LEFT JOIN contacts ON transaction_log.sold_to = contacts.contact_id 
WHERE (SUBSTRING_INDEX(date, ' ', 1) >= DATE_SUB(CURDATE(),INTERVAL 365 DAY) 
AND SUBSTRING_INDEX(date, ' ', 1) <= DATE_SUB(CURDATE(), INTERVAL 0 DAY)) 
AND (transaction_type="Memberships" AND paid=1);
*/
// Membership via volunteering
$query = "SELECT contact_id, full_name, normal_full_name, email, phone, sort_visits, sort_hours FROM 
(SELECT contacts.contact_id, CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name,
CONCAT(first_name, ' ', last_name) AS normal_full_name,
contacts.email AS email, contacts.phone AS phone, 
COUNT(shop_hours.contact_id) as sort_visits, 
ROUND(SUM(HOUR(SUBTIME( TIME(time_out), TIME(time_in))) + MINUTE(SUBTIME( TIME(time_out), TIME(time_in)))/60)) AS sort_hours 
FROM shop_hours 
LEFT JOIN contacts ON shop_hours.contact_id = contacts.contact_id 
LEFT JOIN shop_user_roles ON shop_hours.shop_user_role = shop_user_roles.shop_user_role_id 
WHERE  (SUBSTRING_INDEX(time_in, ' ', 1) >= DATE_SUB(CURDATE(),INTERVAL $days_range1 DAY)  
AND SUBSTRING_INDEX(time_in, ' ', 1) <= DATE_SUB(CURDATE(), INTERVAL $days_range2 DAY))
AND shop_user_roles.volunteer = 1 GROUP BY contact_id) AS members 
WHERE sort_hours >= $membership_hours AND sort_visits >= $membership_days 
GROUP by contact_id ORDER by sort_hours DESC, sort_visits DESC;";
$members = mysql_query($query, $YBDB) or die(mysql_error());
$num_member_rows = mysql_num_rows($members);
// Purchased Membership
$purchase_query = "SELECT contacts.contact_id, 
CONCAT(last_name, ', ', first_name, ' ',middle_initial) AS full_name, 
CONCAT(first_name, ' ', last_name) AS normal_full_name,
contacts.email AS email, contacts.phone AS phone, 
transaction_log.date AS sort_hours, SUBSTRING_INDEX(DATE_ADD(date, INTERVAL 365 DAY), ' ', 1) AS expiration_date
FROM transaction_log 
LEFT JOIN contacts ON transaction_log.sold_to = contacts.contact_id 
WHERE SUBSTRING_INDEX(date, ' ', 1) <= DATE_ADD(date, INTERVAL 365 DAY) 
AND (transaction_type='Memberships' AND paid=1);";
$purchased_membership = mysql_query($purchase_query, $YBDB) or die(mysql_error());
?>
        
        
	  	  
        
          | 
              
                | Membership [= $membership_days days and >= $membership_hours hours" ?>] |  
			    | Members 
 | Visits 
 | Hours 
 |  
            	            
             | "> (paid until )
 | "> |  |  |  | 
     		
   		
     		
			
     		
     		
     		Date Range: 
     		
     		
     		
			
     		Contact Information
			Email Address: 
			 
		
			
		
			
			Phone Numbers for Members Without Email Addresses
			";
					} else {
						echo $result['normal_full_name'] . ", " . $result['phone'];
					}
				}
				
				$c++;
			?> 
			
			
			
			CSV
			Name, Phone, Email
			";
					} else {
						echo $result['normal_full_name'] . ", " . $result['phone'] . ", ". $result['email'];
					}
				
				$c++;
			?>