Example #1
0
 public function testDateIsCreatedWithCustomTimeString()
 {
     $date = new ExpressiveDate('31 January 1991');
     $this->assertEquals('31/01/1991', $date->format('d/m/Y'));
     $date = new ExpressiveDate('+1 day');
     $this->assertEquals(time() + 86400, $date->getTimestamp());
     $date = new ExpressiveDate('-1 day');
     $this->assertEquals(time() - 86400, $date->getTimestamp());
 }
Example #2
0
function eme_localised_date($mydate,$date_format='') {
   global $eme_date_format, $eme_timezone;
   if (empty($date_format))
      $date_format = $eme_date_format;
   // $mydate contains the timezone, but in case it doesn't we provide it
   $eme_date_obj = new ExpressiveDate($mydate,$eme_timezone);
   // Currently in the backend, the timezone is UTC, but maybe that changes in future wp versions
   //   so we search for the current timezone using date_default_timezone_get
   // Since DateTime::format doesn't respect the locale, we use date_i18n here
   //   but date_i18n uses the WP backend timezone, so we need to account for the timezone difference
   // All this because we don't want to use date_default_timezone_set() and wp doesn't set the backend
   //   timezone correctly ...
   $wp_date = new ExpressiveDate($eme_date_obj->getDateTime(),date_default_timezone_get());
   $tz_diff=$eme_date_obj->getOffset()-$wp_date->getOffset();
   $result = date_i18n($date_format, $eme_date_obj->getTimestamp()+$tz_diff);
   return $result;
}
 /**
  * Get a relative date string, e.g., 3 days ago.
  *
  * @param  ExpressiveDate  $compare
  * @return string
  */
 public function getRelativeDate($compare = null)
 {
     if (!$compare) {
         $compare = new ExpressiveDate(null, $this->getTimezone());
     }
     $units = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
     $values = array(60, 60, 24, 7, 4.35, 12);
     // Get the difference between the two timestamps. We'll use this to cacluate the
     // actual time remaining.
     $difference = abs($compare->getTimestamp() - $this->getTimestamp());
     for ($i = 0; $i < count($values) and $difference >= $values[$i]; $i++) {
         $difference = $difference / $values[$i];
     }
     // Round the difference to the nearest whole number.
     $difference = round($difference);
     if ($compare->getTimestamp() < $this->getTimestamp()) {
         $suffix = 'from now';
     } else {
         $suffix = 'ago';
     }
     // Get the unit of time we are measuring. We'll then check the difference, if it is not equal
     // to exactly 1 then it's a multiple of the given unit so we'll append an 's'.
     $unit = $units[$i];
     if ($difference != 1) {
         $unit .= 's';
     }
     return $difference . ' ' . $unit . ' ' . $suffix;
 }