Support » Plugins » Problem displaying correct date in homemade widget

  • Hello,

    I just put together my first widget via a plugin that pulls in the closest upcoming event. The event date is a custom fields date field (using Flutter). The code works fine for me as part of a page template, but once I put it into the plugin/widget the date doesn’t display correctly. Here is the date code that’s not working for me:

    echo date('l, F j, Y', mktime(0,0,0,$etest[1],$etest[2],$etest[0]))

    Here is the full plugin code (note I had to add “(int)” to the Year because of an error message I was getting on number length):

    <?php
    /*
    Plugin Name: Upcoming Event Widget
    Plugin URI: http://www.joshofsorts.com/
    Description: Widget that shows upcoming events and exhibits at the museum.
    Author: Josh McCool
    Version: 1.0
    Author URI: http://www.joshofsorts.com/
    */
    
    function upcoming_widget() {
      echo "<div class='current-box'>";
      echo "<b>Next Event</b><br />";
      $todaysDate = date('Y-m-d');
      query_posts('showposts=1&category_name=events&meta_key=event_start&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
      if ( have_posts() ) : while ( have_posts() ) : the_post();
      $sdat = get_post_meta($post->ID, 'event_start', true);
      $stest = explode('-',$sdat);
      $edat = get_post_meta($post->ID, 'event_end', true);
      $etest = explode('-',$edat);
      echo "<div class='event_date'>";
      echo date('l, F j, Y', mktime(0,0,0,$stest[1],$stest[2],(int)$stest[0])); unset($stest); $startdate = get_post_meta($post->ID, "event_start", $single = true); $enddate = get_post_meta($post->ID, "event_end", $single = true); if ( $enddate == $startdate ) {echo "";} else {echo " - "; echo date('l, F j, Y', mktime(0,0,0,$etest[1],$etest[2],(int)$etest[0])); unset($etest);;};
      echo "<br />";
      echo get('event_times'); $starttime = get_post_meta($post->ID, "event_times", $single = true); $endtime = get_post_meta($post->ID, "event_timee", $single = true); if ( $endtime == $starttime ) {echo "";} else {echo " - "; echo get('event_timee');};
      echo "</div>";
      echo "<div class='event_title'>"; echo get('event_name'); echo "</div>";
      echo "<div class='event_sum'>"; echo get('event_desc'); echo "</div>";
      endwhile; else:
      echo "<p>"; _e('Sorry, no posts matched your criteria.'); echo "</p>";
      endif;
      echo "<br />";
      echo "<b>Feature Exhibit</b><br />";
      query_posts(array('category_name' => exhibit, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post();
      echo "<div class='event_title'>"; echo get('event_name'); echo "</div>";
      echo "<img src='"; echo pt(); ?>?src=<?php echo get('event_thumb'); echo "&f=jpg>&w=250>&h=200>&zc=C' />";
      endwhile; else:
      _e('Sorry, no posts matched your criteria.');
      endif;
      echo "</div>";
    }
    
    function init_upcoming(){
    	register_sidebar_widget("Upcoming Events Widget", "upcoming_widget");
    }
    
    add_action("plugins_loaded", "init_upcoming");
    
    ?>

    In order to sort by the custom field dates I have to save the information as YYYY-MM-DD and then explode the information and reorder it to display as above. Any suggestions? I think it’s time I crack open my Php book and learn more than I have just to get by. Thanks in advance!

  • The topic ‘Problem displaying correct date in homemade widget’ is closed to new replies.