getDifferenceInDays() public method

Get the difference in days.
public getDifferenceInDays ( ExpressiveDate $compare = null ) : string
$compare ExpressiveDate
return string
function eme_update_events_for_recurrence($event, $recurrence)
{
    global $wpdb, $eme_timezone;
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $matching_days = eme_get_recurrence_days($recurrence);
    //print_r($matching_days);
    sort($matching_days);
    $eme_date_obj1 = new ExpressiveDate($event['event_start_date'] . " " . $event['event_start_time'], $eme_timezone);
    if ($event['event_end_date'] == '') {
        $duration_days_event = 0;
    } else {
        $eme_date_obj2 = new ExpressiveDate($event['event_end_date'] . " " . $event['event_end_time'], $eme_timezone);
        $duration_days_event = abs($eme_date_obj2->getDifferenceInDays($eme_date_obj1));
    }
    // 2 steps for updating events for a recurrence:
    // First step: check the existing events and if they still match the recurrence days, update them
    //       otherwise delete the old event
    // Reason for doing this: we want to keep possible booking data for a recurrent event as well
    // and just deleting all current events for a recurrence and inserting new ones would break the link
    // between booking id and event id
    // Second step: check all days of the recurrence and if no event exists yet, insert it
    $sql = $wpdb->prepare("SELECT * FROM {$events_table} WHERE recurrence_id = %d", $recurrence['recurrence_id']);
    $events = $wpdb->get_results($sql, ARRAY_A);
    $eme_date_obj = new ExpressiveDate(null, $eme_timezone);
    // Doing step 1
    foreach ($events as $existing_event) {
        $update_done = 0;
        foreach ($matching_days as $day) {
            $eme_date_obj->setTimestamp($day);
            $event_start_date = $eme_date_obj->getDate();
            if (!$update_done && $existing_event['event_start_date'] == $event_start_date) {
                $event['event_start_date'] = $existing_event['event_start_date'];
                $eme_date_obj->addDays($duration_days_event);
                $event['event_end_date'] = $eme_date_obj->getDate();
                eme_db_update_event($event, $existing_event['event_id'], 1);
                $update_done = 1;
                continue;
            }
        }
        if (!$update_done) {
            eme_db_delete_event($existing_event, 1);
        }
    }
    // Doing step 2
    foreach ($matching_days as $day) {
        $insert_needed = 1;
        $eme_date_obj->setTimestamp($day);
        $event['event_start_date'] = $eme_date_obj->getDate();
        $eme_date_obj->addDays($duration_days_event);
        $event['event_end_date'] = $eme_date_obj->getDate();
        foreach ($events as $existing_event) {
            if ($insert_needed && $existing_event['event_start_date'] == $event['event_start_date']) {
                $insert_needed = 0;
            }
        }
        if ($insert_needed == 1) {
            eme_db_insert_event($event, 1);
        }
    }
    return 1;
}
Example #2
0
function eme_countdown($atts)
{
    global $eme_timezone;
    extract(shortcode_atts(array('id' => ''), $atts));
    if ($id != "") {
        $event = eme_get_event($id);
    } else {
        $newest_event_array = eme_get_events(1);
        $event = $newest_event_array[0];
    }
    $eme_date_obj = new ExpressiveDate($event['event_start_date'] . " " . $event['event_start_time'], $eme_timezone);
    $eme_date_obj_now = new ExpressiveDate(null, $eme_timezone);
    return intval($eme_date_obj_now->getDifferenceInDays($eme_date_obj));
}
Example #3
0
 public function makeTasks($tasks)
 {
     try {
         $data = array();
         foreach ($tasks as $task) {
             if ($task['status'] == 'delayed') {
                 $today = new ExpressiveDate();
                 $enddate = new ExpressiveDate($task['end_date']);
                 $task['num_status'] = (int) $today->getDifferenceInDays($enddate);
                 if ($task['num_status'] > 0) {
                     $task['status'] = 'active';
                     $tempTask = \Task::find($task['id']);
                     $tempTask->status = 'active';
                     $tempTask->save();
                 } else {
                     $task['status_desc'] = abs($task['num_status']) . ' days passed since End date';
                 }
             } elseif ($task['status'] == 'active') {
                 if ($task['end_date'] != null) {
                     $today = new ExpressiveDate();
                     $enddate = new ExpressiveDate($task['end_date']);
                     $task['num_status'] = (int) $today->getDifferenceInDays($enddate);
                     if ((int) $task['num_status'] < 0) {
                         $tempTask = \Task::find($task['id']);
                         $tempTask->status = 'delayed';
                         $tempTask->update();
                         $task['status_desc'] = abs($task['num_status']) . ' days passed since End date';
                         $task['num_status'] = 'Delayed';
                         $task['status'] = 'delayed';
                     }
                 } else {
                     $task['num_status'] = 'Active';
                 }
             }
             if ($task['project_id'] == null) {
                 $task['project_name'] = null;
             } else {
                 $project = \Project::find($task['project_id']);
                 $task['project_name'] = $project->project_name;
             }
             $tempUpdatedAt = new ExpressiveDate($task['updated_at']);
             $task['updated_at'] = $tempUpdatedAt->format('jS F, Y \\a\\t g:ia');
             $task['totalsubtasks'] = $subtasks = \Task::find($task['id'])->subtasks()->get()->count();
             if ($task['totalsubtasks'] == 0) {
                 $task['rem_subtasks'] = 0;
                 $task['subTaskPercentage'] = 0;
             } else {
                 $task['rem_subtasks'] = $subtasks = \Task::find($task['id'])->subtasks()->where('status', '=', 'active')->orWhere('status', '=', 'delayed')->get()->count();
                 $task['subTaskPercentage'] = (int) (($task['totalsubtasks'] - $task['rem_subtasks']) * 100) / $task['totalsubtasks'];
             }
             $task['users'] = \Task::find($task['id'])->users()->orderBy('first_name')->get()->toArray();
             $task['files'] = \Fileref::where('parent_id', '=', $task['id'])->where('parent_type', '=', 'task')->get()->count();
             $data[] = $task;
         }
         return $data;
     } catch (Exception $e) {
         \Log::error('Something went wrong in Task Repository - makeTasks():' . $e->getMessage());
         throw new \SomeThingWentWrongException();
     }
 }
Example #4
0
 public function testGetDateDifferenceInDays()
 {
     $past = new ExpressiveDate('January 12');
     $future = new ExpressiveDate('February 15');
     $this->assertEquals(-34, $future->getDifferenceInDays($past));
     $this->assertEquals(34, $past->getDifferenceInDays($future));
 }