Skip to content

Instantly share code, notes, and snippets.

@ockham
Created December 28, 2012 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ockham/4396952 to your computer and use it in GitHub Desktop.
Save ockham/4396952 to your computer and use it in GitHub Desktop.
<?php
/**
* Date Functions
*
* Display functions (template-tags) for use in WordPress templates.
*/
// Don't load directly
if ( !defined('ABSPATH') ) { die('-1'); }
if( class_exists( 'TribeEvents' ) ) {
/**
* Start Date
*
* Returns the event start date and time
*
* @param int $postId (optional) This only works for non recurring events
* @param bool $displayTime If true shows date and time, if false only shows date
* @param string $dateFormat Allows date and time formating using standard php syntax (http://php.net/manual/en/function.date.php)
* @return string Date
* @todo support $postId for recurring events.
* @since 2.0
*/
function tribe_get_start_date( $postId = null, $displayTime = true, $dateFormat = '' ) {
$postId = TribeEvents::postIdHelper( $postId );
if (!$postId || ( function_exists('tribe_is_recurring_event') && tribe_is_recurring_event( $postId ) ) ) {
global $post;
} else {
$post = get_post($postId);
}
if( tribe_get_all_day( $postId ) )
$displayTime = false;
if( empty($post->EventStartDate) )
$post->EventStartDate = tribe_get_event_meta( $postId, '_EventStartDate', true );
if( isset($post->EventStartDate) ){
$date = strtotime( $post->EventStartDate );
}else{
return; // '&mdash;';
}
return tribe_event_format_date($date, $displayTime, $dateFormat );
}
/**
* End Date
*
* Returns the event end date
*
* @param int $postId (optional) this only works for non recurring events
* @param bool $displayTime If true shows date and time, if false only shows date
* @param string $dateFormat Allows date and time formating using standard php syntax (http://php.net/manual/en/function.date.php)
* @return string Date
* @todo support $postId for recurring events.
* @since 2.0
*/
function tribe_get_end_date( $postId = null, $displayTime = 'true', $dateFormat = '', $displayDate = true ) {
$postId = TribeEvents::postIdHelper( $postId );
if (!$postId || ( function_exists('tribe_is_recurring_event') && tribe_is_recurring_event( $postId ) ) ) {
global $post;
} else {
$post = get_post($postId);
}
if( tribe_get_all_day( $postId ) )
$displayTime = false;
if( empty($post->EventEndDate) )
$post->EventEndDate = tribe_get_event_meta( $postId, '_EventEndDate', true );
if( isset($post->EventEndDate) ){
$date = strtotime( $post->EventEndDate );
}else{
return; // '&mdash;';
}
return tribe_event_format_date($date, $displayTime, $dateFormat, $displayDate );
}
/**
* Formatted Date
*
* Returns formatted date
*
* @param string $date
* @param bool $displayTime If true shows date and time, if false only shows date
* @param string $dateFormat Allows date and time formating using standard php syntax (http://php.net/manual/en/function.date.php)
* @return string
* @since 2.0
*/
function tribe_event_format_date($date, $displayTime = true, $dateFormat = '', $displayDate = true) {
$tribe_ecp = TribeEvents::instance();
if( $displayDate )
if( $dateFormat ) $format = $dateFormat;
else $format = get_option( 'date_format', TribeDateUtils::DATEONLYFORMAT );
if ( $displayTime )
$format = $tribe_ecp->getTimeFormat( $format );
$shortMonthNames = ( strstr( $format, 'M' ) ) ? true : false;
$date = date_i18n ( $format, $date );
return str_replace( array_keys($tribe_ecp->monthNames( $shortMonthNames )), $tribe_ecp->monthNames( $shortMonthNames ), $date);
}
}
?>
<?php
/**
* This is the template for the output of the events list widget.
* All the items are turned on and off through the widget admin.
* There is currently no default styling, which is highly needed.
*
* You can customize this view by putting a replacement file of the same name (events-list-load-widget-display.php) in the events/ directory of your theme.
*
* @return string
*/
// Vars set:
// '$event->AllDay',
// '$event->StartDate',
// '$event->EndDate',
// '$event->ShowMapLink',
// '$event->ShowMap',
// '$event->Cost',
// '$event->Phone',
// Don't load directly
if ( !defined('ABSPATH') ) { die('-1'); }
$event = array();
$tribe_ecp = TribeEvents::instance();
reset($tribe_ecp->metaTags); // Move pointer to beginning of array.
foreach($tribe_ecp->metaTags as $tag){
$var_name = str_replace('_Event','',$tag);
$event[$var_name] = tribe_get_event_meta( $post->ID, $tag, true );
}
$event = (object) $event; //Easier to work with.
ob_start();
if ( !isset($alt_text) ) { $alt_text = ''; }
post_class($alt_text,$post->ID);
$class = ob_get_contents();
ob_end_clean();
?>
<li <?php echo $class ?>>
<div class="when">
<?php
$space = false;
$output = '';
echo tribe_get_start_date( $post->ID );
if( tribe_is_multiday( $post->ID ) ) {
echo ' – <br/>'. tribe_get_end_date($post->ID, 'true', '', true);
}
elseif( !$event->AllDay ) {
echo ' – '. tribe_get_end_date($post->ID, 'true', '', false);
}
if( $event->AllDay ) {
echo ' <small><em>('.__('All Day','tribe-events-calendar').')</em></small>';
}
?>
</div>
<div class="event">
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a>
</div>
</li>
<?php $alt_text = ( empty( $alt_text ) ) ? 'alt' : ''; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment