I have updated the code to work with single-day events, events that have already started but havn't ended, etc. Thanks to
tomsch for the mkdate snippet!
I believe I got user time zone correction to work. Please see the following code:
Note: This version checks for the user's preferred date and time display settings (i.e. UK vs US, etc) and displays things accordingly. This can also pull from one or more selected calendars, per the instructions below (see orange comments).
PHP Code:
ob_start();
//Change $show_count to change how many events to display in the block
$show_count = 5;
//Get the user's time zone
$user_tz = vB::$vbulletin->userinfo['timezoneoffset'];
$tz_adjust = $user_tz * 3600;
$mydate = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
//Get events from the table
//Use the code below to pull from ALL calendars
$query = sprintf(" SELECT * FROM ".TABLE_PREFIX."event WHERE visible = 1 AND (dateline_from >= '$mydate' || (dateline_from >= '$mydate' AND dateline_to <= '$mydate' ) || ('$mydate' BETWEEN dateline_from AND dateline_to)) ORDER BY dateline_from ASC LIMIT $show_count");
//To pull from just certain calendars, comment out the above $query and uncomment the line below. Change the "AND calendarids in (1, 2, 3)" to match your calendar IDs to pull
//$query = sprintf(" SELECT * FROM ".TABLE_PREFIX."event WHERE visible = 1 AND (dateline_from >= '$mydate' || (dateline_from >= '$mydate' AND dateline_to <= '$mydate' ) || ('$mydate' BETWEEN dateline_from AND dateline_to)) AND calendarids in (1, 2, 3) ORDER BY dateline_from ASC LIMIT $show_count");
$event_get = vB::$db->query_read($query);
$output_bits = '';
while($event = vB::$db->fetch_array($event_get)) {
// Convert user timezone offset into minutes
$offset = $user_tz;
// Only adjust start times for ranged events; all-day events should keep their midnight start time.
if($event['dateline_to'] == 0 )
{
$from = $event['dateline_from'];
} else {
$from = $event['dateline_from'] + $tz_adjust;
}
$to = $event['dateline_to'] + $tz_adjust;
$fromDate = trim(vbdate(vB::$vbulletin->options['dateformat'], $from, false, true, false, true));
$toDate = trim(vbdate(vB::$vbulletin->options['dateformat'], $to, false, true, false, true));
$fromTime = trim(vbdate(vB::$vbulletin->options['timeformat'], $from, false, true, false, true));
$toTime = trim(vbdate(vB::$vbulletin->options['timeformat'], $to, false, true, false, true));
if($event['dateline_to'] == 0 )
{
$format = sprintf("%s",$fromDate);
} else {
$format = sprintf("%s %s - %s",$fromDate, $fromTime, $toTime);
}
$output_bits .= sprintf('
<div class = "cms_widget_post_bit"><h4 class="cms_widget_post_header"><a href="calendar.php?do=getinfo&e=%d">%s</a></h4>
<p class="cms_widget_post_content">%s</p>
</div>
',$event['eventid'],$event['title'],$format);
}
$output = $output_bits;
ob_end_clean();