Example #1
0
 /**
  * This function finds the difference between two timestamps and constructs
  * a
  * human-readable string for it.
  *
  * @access private
  * @author arnold:tserepov <tserepov@gmail.com.
  * @param string $parameters['timestampInPast']
  *        	the timestamp in the past in the format
  *        	<code>/\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}/u</code>
  * @param string $parameters['timestampInFuture']
  *        	the timestamp in the future in the format
  *        	<code>/\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}/u</code>
  * @return string the human-readable string
  */
 private static function findDifference($parameters)
 {
     if (isset($parameters['timestampInPast'])) {
         try {
             $timestampInPast = new \DateTime($parameters['timestampInPast']);
         } catch (\Exception $e) {
             // @formatter:off
             die(sprintf('Varasem ajatempel ei ole sobivas vormingus. Veateade: %1$s Teekond vea olukorrani: %2$s', $e->getMessage(), $e->getTraceAsString()));
             // @formatter:on
         }
     } else {
         $timestampInPast = new \DateTime();
     }
     if (isset($parameters['timestampInFuture'])) {
         try {
             $timestampInFuture = new \DateTime($parameters['timestampInFuture']);
         } catch (Exception $e) {
             // @formatter:off
             die(\sprintf('Hilisem ajatempel ei ole sobivas vormingus. Veateade: %1$s Teekond vea olukorrani: %2$s', $e->getMessage(), $e->getTraceAsString()));
             // @formatter:on
         }
     } else {
         $timestampInFuture = new \DateTime();
     }
     if ($timestampInFuture->getTimestamp() > $timestampInPast->getTimestamp()) {
         $difference = $timestampInPast->diff($timestampInFuture);
     } else {
         $difference = $timestampInFuture->diff($timestampInPast);
     }
     $partsOfTime = array();
     if ($difference->y > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->y, 'unitInSingular' => \pstk\String::translate('year'), 'unitInPlural' => \pstk\String::translate('years')));
     }
     if ($difference->m > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->m, 'unitInSingular' => \pstk\String::translate('month'), 'unitInPlural' => \pstk\String::translate('months')));
     }
     if ($difference->d > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->d, 'unitInSingular' => \pstk\String::translate('day'), 'unitInPlural' => \pstk\String::translate('days')));
     }
     if ($difference->h > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->h, 'unitInSingular' => \pstk\String::translate('hour'), 'unitInPlural' => \pstk\String::translate('hours')));
     }
     if ($difference->i > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->i, 'unitInSingular' => \pstk\String::translate('minute'), 'unitInPlural' => \pstk\String::translate('minutes')));
     }
     if ($difference->s > 0) {
         $partsOfTime[] = \tuts\View::generatePartialString(array('time' => $difference->s, 'unitInSingular' => \pstk\String::translate('second'), 'unitInPlural' => \pstk\String::translate('seconds')));
     }
     $humanReadableString = implode(" ", $partsOfTime);
     // echo ' <br/>201: ', $humanReadableString;
     return $humanReadableString;
 }