public function testCheckDateTime()
 {
     $date = new DateTime('2013-12-31 15:00');
     $this->assertEquals($date, _eventorganiser_check_datetime('2013-12-31 15:00', 'Y-m-d'));
     $this->assertEquals($date, _eventorganiser_check_datetime('31-12-2013 15:00', 'd-m-Y'));
     $this->assertEquals($date, _eventorganiser_check_datetime('12-31-2013 15:00', 'm-d-Y'));
     //Without time
     $this->assertEquals(false, _eventorganiser_check_datetime('2013-12-31', 'Y-m-d'));
     //With seconds
     $this->assertEquals($date, _eventorganiser_check_datetime('2013-12-31 15:00:00', 'Y-m-d'));
 }
/**
 * Returns formatted date of an event archive.
 *
 * Returns the formatted ate of an event archive. E.g. for date archives, returns that date, 
 * for year archives returns 1st January of that year, for month archives 1st of that month.
 * The date is formatted according to `$format` via {@see `eo_format_datetime()`}
 *
 * <code>
 * 	<?php
 *	 if( eo_is_event_archive('day') )
 *      //Viewing date archive: "Events: 3rd June 2013"
 *      echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('jS F Y');
 *	 elseif( eo_is_event_archive('month') )
 *      //Viewing month archive: "Events: June 2013"
 *      echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('F Y');
 *   elseif( eo_is_event_archive('year') )
 *      //Viewing year archive: "Events: 2013"
 *      echo __('Events: ','eventorganiser').' '.eo_get_event_archive_date('Y');
 *   else
 *      _e('Events','eventorganiser');
 *   ?>
 * </code>
 * @since 1.7
 * @uses is_post_type_archive()
 * @uses eo_format_datetime()
 * @link http://php.net/manual/en/function.date.php Formatting dates
 * @param string|constant $format How to format the date, see http://php.net/manual/en/function.date.php  or DATETIMEOBJ constant to return the datetime object.
 * @return string|dateTime The formatted date
*/
function eo_get_event_archive_date($format = DATETIMEOBJ)
{
    if (!is_post_type_archive('event')) {
        return false;
    }
    $ondate = str_replace('/', '-', get_query_var('ondate'));
    $parts = count(explode('-', $ondate));
    if ($parts == 1 && is_numeric($ondate)) {
        //Numeric - interpret as year
        $ondate .= '-01-01';
    } elseif ($parts == 2) {
        // 2012-01 format: interpret as month
        $ondate .= '-01';
    }
    $ondate = _eventorganiser_check_datetime($ondate . ' 00:00', 'Y-m-d');
    return eo_format_datetime($ondate, $format);
}
/**
 * Saves the event data posted from the event metabox.
 * Hooked to the 'save_post' action
 * 
 * @since 1.0.0
 *
 * @param int $post_id the event post ID
 * @return int $post_id the event post ID
 */
function eventorganiser_details_save($post_id)
{
    //make sure data came from our meta box
    if (!isset($_POST['_eononce']) || !wp_verify_nonce($_POST['_eononce'], 'eventorganiser_event_update_' . $post_id)) {
        return;
    }
    // verify this is not an auto save routine.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    //authentication checks
    if (!current_user_can('edit_event', $post_id)) {
        return;
    }
    //Collect raw data
    $raw_data = isset($_POST['eo_input']) ? $_POST['eo_input'] : array();
    $raw_data = wp_parse_args($raw_data, array('StartDate' => '', 'EndDate' => '', 'StartTime' => '00:00', 'FinishTime' => '23:59', 'schedule' => 'once', 'event_frequency' => 1, 'schedule_end' => '', 'allday' => 0, 'schedule_meta' => '', 'days' => array(), 'include' => '', 'exclude' => ''));
    //Update venue
    $venue_id = !empty($raw_data['event-venue']) ? intval($raw_data['event-venue']) : null;
    //Maybe create a new venue
    if (empty($venue_id) && !empty($_POST['eo_venue']) && current_user_can('manage_venues')) {
        $venue = $_POST['eo_venue'];
        if (!empty($venue['name'])) {
            $new_venue = eo_insert_venue($venue['name'], $venue);
            if (!is_wp_error($new_venue)) {
                $venue_id = $new_venue['term_id'];
            }
        }
    }
    //Set venue
    $r = wp_set_post_terms($post_id, array($venue_id), 'event-venue', false);
    //If reocurring, but not editing occurrences, can abort here, but trigger hook.
    if (eo_reoccurs($post_id) && (!isset($raw_data['AlterRe']) || 'yes' != $raw_data['AlterRe'])) {
        do_action('eventorganiser_save_event', $post_id);
        //Need this to update cache
        return;
    }
    //Set times for all day events
    $all_day = intval($raw_data['allday']);
    if ($all_day) {
        $raw_data['StartTime'] = '00:00';
        $raw_data['FinishTime'] = '23:59';
    } elseif (!eventorganiser_blog_is_24()) {
        //Potentially need to parse 24
        //TODO incorproate into _eventorganiser_check_datetime
        $raw_data['StartTime'] = date("H:i", strtotime($raw_data['StartTime']));
        $raw_data['FinishTime'] = date("H:i", strtotime($raw_data['FinishTime']));
    }
    //Check dates
    $start = _eventorganiser_check_datetime(trim($raw_data['StartDate']) . ' ' . trim($raw_data['StartTime']));
    $end = _eventorganiser_check_datetime(trim($raw_data['EndDate']) . ' ' . trim($raw_data['FinishTime']));
    $schedule_last = _eventorganiser_check_datetime(trim($raw_data['schedule_end']) . ' ' . trim($raw_data['StartTime']));
    //Collect schedule meta
    $schedule = $raw_data['schedule'];
    if ('weekly' == $schedule) {
        $schedule_meta = $raw_data['days'];
        $occurs_by = '';
    } elseif ('monthly' == $schedule) {
        $schedule_meta = $raw_data['schedule_meta'];
        $occurs_by = trim($schedule_meta, '=');
    } else {
        $schedule_meta = '';
        $occurs_by = '';
    }
    //Collect include/exclude
    $in_ex = array();
    foreach (array('include', 'exclude') as $key) {
        $in_ex[$key] = array();
        $arr = explode(',', sanitize_text_field($raw_data[$key]));
        if (!empty($arr)) {
            foreach ($arr as $date) {
                $date_obj = _eventorganiser_check_datetime($date . ' ' . $raw_data['StartTime'], 'Y-m-d');
                if ($date_obj) {
                    $in_ex[$key][] = $date_obj;
                }
            }
        }
    }
    $event_data = array('start' => $start, 'end' => $end, 'all_day' => $all_day, 'schedule' => $schedule, 'frequency' => (int) $raw_data['event_frequency'], 'schedule_last' => $schedule_last, 'schedule_meta' => $schedule_meta, 'occurs_by' => $occurs_by, 'include' => $in_ex['include'], 'exclude' => $in_ex['exclude']);
    $response = eo_update_event($post_id, $event_data);
    if (is_wp_error($response)) {
        global $EO_Errors;
        $code = $response->get_error_code();
        $message = $response->get_error_message($code);
        $errors[$post_id][] = __('Event dates were not saved.', 'eventorganiser');
        $errors[$post_id][] = $message;
        $EO_Errors->add('eo_error', $message);
        update_option('eo_notice', $errors);
    }
    return;
}