Back to index

php5  5.3.10
Enumerations | Functions
examine_diff.inc File Reference

Go to the source code of this file.

Enumerations

enum  PHPT_DATETIME_SHOW_DIFF
 Helper for the DateTime diff/days/math tests. More...
enum  PHPT_DATETIME_SHOW_DAYS
enum  PHPT_DATETIME_SHOW_ADD
enum  PHPT_DATETIME_SHOW_SUB

Functions

 examine_diff ($end_date, $start_date, $expect_spec, $expect_days, $absolute=false)
 

@-



Enumeration Type Documentation

Definition at line 14 of file examine_diff.inc.

Definition at line 13 of file examine_diff.inc.

Helper for the DateTime diff/days/math tests.

Author:
Daniel Convissor danie.nosp@m.lc@a.nosp@m.nalys.nosp@m.isan.nosp@m.dsolu.nosp@m.tion.nosp@m.s.com#@+ Which test segments should be displayed?

Definition at line 12 of file examine_diff.inc.

Definition at line 15 of file examine_diff.inc.


Function Documentation

examine_diff ( end_date,
start_date,
expect_spec,
expect_days,
absolute = false 
)

@-

Provides a consistent interface for executing date diff/add/sub tests

Parameters:
string | DateTime$end_datethe end date in YYYY-MM-DD format (can include time HH:MM:SS) or a DateTime object
string | DateTime$start_datethe start date in YYYY-MM-DD format (can include time HH:MM:SS) or a DateTime object
string$expect_specthe expected result of the tests, in the special interval specification used for this test suite. This spec includes a "+" or "-" after the "P" in order to indicate which direction to go.
int$expect_daysthe number of days to compare with the interval's "days" property
bool$absoluteshould the result always be a positive number?
Returns:
void

Definition at line 35 of file examine_diff.inc.

                                                                                             {
       if (is_string($start_date)) {
              $start = new DateTime($start_date);
       } else {
              $start = $start_date;
       }
       $start_date = $start->format('Y-m-d H:i:s T');

       if (is_string($end_date)) {
              $end = new DateTime($end_date);
       } else {
              $end = $end_date;
       }
       $end_date = $end->format('Y-m-d H:i:s T');

       $expect_interval = new DateInterval('P' . substr($expect_spec, 2));
       if (substr($expect_spec, 1, 1) == '-') {
              $expect_interval->invert = true;
       }

       if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_DIFF) {
              $result_interval = $start->diff($end, $absolute);
              $result_spec = $result_interval->format('P%R%yY%mM%dDT%hH%iM%sS');
              echo "DIFF: $end_date - $start_date = **$result_spec**\n";
              // echo "DIFF: $end_date - $start_date = **$expect_spec**\n";
       }
       if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_DAYS) {
              $result_interval = $start->diff($end, $absolute);
              $result_days = $result_interval->format('%a');
              echo "DAYS: **$result_days**\n";
              // echo "DAYS: **$expect_days**\n";
       }
       if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_ADD) {
              $start->add($expect_interval);
              $result_end_date = $start->format('Y-m-d H:i:s T');
              echo "ADD: $start_date + $expect_spec = **$result_end_date**\n";
              // echo "ADD: $start_date + $expect_spec = **$end_date**\n";
       }
       if (PHPT_DATETIME_SHOW == PHPT_DATETIME_SHOW_SUB) {
              $end->sub($expect_interval);
              $result_start_date = $end->format('Y-m-d H:i:s T');
              echo "SUB: $end_date - $expect_spec = **$result_start_date**\n";
              // echo "SUB: $end_date - $expect_spec = **$start_date**\n";
       }
}