Returns the timestamp for the most recent (previous) occurance of [day].

@param	timestamp
@param	int			the day of week
@return	timestamp
338501
public static function previousDay($ts = null, $day = sfTime::SUNDAY)
	{
		// default to now
		if ($ts === null) $ts = sfDateTimeToolkit::now();
		
		// get offsets from sunday
		$offset1 = date('w', $ts);
		$offset2 = $day;
		
		// adjust if date wraps into last week
		$offset1 += $offset1 > $offset2 ? 0 : 7;
		
		return sfTime::subtractDay($ts, $offset1 - $offset2);
	}
1149036
private function check_timestamp($timestamp)
    {
        if( ! $timestamp )
            throw new Exception(
                'Missing timestamp parameter. The parameter is required'
            );

        // verify that timestamp is recentish
        $now = time();
        if (abs($now - $timestamp) > $this->timestamp_threshold) {
            throw new Exception(
                "Expired timestamp, yours $timestamp, ours $now"
            );
        }
    }
633341
public static function timeDiff($timestamp)
    {
        $dateRef = new \DateTime('@' . $timestamp);
        $dateNow = new \DateTime();

        $dateDiff = $dateNow->diff($dateRef);

        return [
        'past_time'  => $dateDiff->invert,
        'year'       => $dateDiff->y,
        'month'      => $dateDiff->m,
        'day'        => $dateDiff->d,
        'hour'       => $dateDiff->h,
        'minute'     => $dateDiff->i,
        'second'     => $dateDiff->s,
        'days_total' => $dateDiff->days,
        ];
    }
585229
public function setTimestamp($timestamp = null)
    {
        $this->timestamp = $timestamp;
        if ($timestamp === null) {
            $this->timestamp = new Timestamp();
        }
        return $this;
    }
696065
public function getLastOccurrence($before_timestamp = null) {
		if ($before_timestamp === null) {
			$before_timestamp = time();
		}
		
		$before_timestamp = (int) $before_timestamp;
		
		$prev = false;
		if ($this->isRecurring()) {
			$starttimes = $this->getStartTimes($this->getStartTimestamp(), $before_timestamp);
			
			if ($starttimes) {
				$prev = end($starttimes);
			}
		}
		else {
			$prev = $this->getStartTimestamp();
			
			if ($prev > $before_timestamp) {
				$prev = false; // there is no occurrence before the timestamp
			}
		}
		
		return $prev;
	}
1318156
public static function ToDate($Timestamp = '') {
      if ($Timestamp == '')
         $Timestamp = time();
      elseif (!is_numeric($Timestamp))
         $Timestamp = self::ToTimestamp($Timestamp);

      return date('Y-m-d', $Timestamp);
   }
418793
private function formatTimestampFromApi(array $timestamp)
    {
        $timestamp += [
            'seconds' => 0,
            'nanos' => 0
        ];

        $dt = $this->createDateTimeFromSeconds($timestamp['seconds']);

        return $this->formatTimeAsString($dt, $timestamp['nanos']);
    }
430489
public function elapsedTime($timestamp)
    {
        if ($timestamp instanceof DateTime) {
            $timestamp = $timestamp->format('U');
        } else if (is_string($timestamp)) {
            $timestamp = strtotime($timestamp);
        }
        
        $diff = time() - $timestamp; 
        $periods = self::$texts['times']; 
        $lengths = array(60, 60, 24, 7, 4.35, 12, 10); 
        $ending = self::$texts['ago'];
        
        if ($diff < 60) {
            return self::$texts['less_than_a_minute'];
        } else if ($diff < 0) {
            $diff = -$diff; 
            $ending = self::$texts['togo']; 
        }
              
        for($j = 0; $j < count($lengths) && $diff >= $lengths[$j]; $j++) {
            $diff /= $lengths[$j]; 
        }
        $diff = round($diff); 
        if($diff != 1) {
            $periods[$j].= "s"; 
        }
        
        if (self::$agoAfter) {
            return "$diff $periods[$j] $ending";
        } else {
            return "$ending $diff $periods[$j]";
        }
    }
11321
private function timestampHasExpired($timestamp) {
        $year   = substr($timestamp, 0, 4);
        $month  = substr($timestamp, 5, 2);
        $day    = substr($timestamp, 8, 2);
        $hour   = substr($timestamp, 11, 2);
        $minute = substr($timestamp, 14, 2);
        $second = substr($timestamp, 17, 2);

        $timestamp = gmmktime($hour, $minute, $second, $month, $day, $year);

        $diff = time() - $timestamp;

        if ($diff > $this->maxDiff || $diff < -$this->maxDiff) {
            return true;
        }

        return false;
    }
966897
public static function unix2dos(int $timestamp = 0) : int
    {
        $timestamp = ($_timestamp == 0) ? getdate() : getdate($_timestamp);

        if ($timestamp['year'] < 1980) return (1 << 21 | 1 << 16);

        $timestamp['year'] -= 1980;

        return ($timestamp['year']    << 25 | $timestamp['mon']     << 21 |
                $timestamp['mday']    << 16 | $timestamp['hours']   << 11 |
                $timestamp['minutes'] << 5  | $timestamp['seconds'] >> 1);
    }
837675
public static function longDate($timestamp = null)
    {
        $l = self::getLocale();
        return self::date($l->shortDateTime, $timestamp);
    }
306758
public static function shortDate($timestamp = null)
    {
        $l = self::getLocale();
        $cYear = date('Y');
        $date = date('Y-m-d', $timestamp);
        if (date('Y-m-d') == $date) {
            return I18n::get('Today %1', self::formatTime($timestamp));
        } elseif (date('Y-m-d', strtotime('yesterday')) == $date) {
            return I18n::get('Yesterday %1', self::formatTime($timestamp));
        } elseif (date('Y-m-d', strtotime('tomorrow')) == $date) {
            return I18n::get('Tomorrow %1', self::formatTime($timestamp));
        } elseif ($cYear == date('Y', $timestamp)) {
            return self::date(I18n::get('F j'), $timestamp);
        } else {
            return self::date(I18n::get('F Y'), $timestamp);
        }
    }
293244
protected function _ago($timestamp, $current_time=null)
	{
		// Store the current time
		if (is_null($current_time))
		{
			// Get now
			$current_time = new self('now');
			$current_time = strtotime($current_time);
		}

		// Determine the difference, between the time now and the timestamp
		$difference = $current_time - $timestamp;

		// Set the periods of time
		$periods = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');

		// Set the number of seconds per period
		$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);

		// Determine which period we should use, based on the number of seconds lapsed.
		// If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
		// Go from decades backwards to seconds
		for ($val = count($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--)
		{
			// Do nothing...
		}

		// Ensure the script has found a match
		if ($val < 0)
		{
			$val = 0;
		}

		// Determine the minor value, to recurse through
		$new_time = $current_time - ($difference % $lengths[$val]);

		// Set the current value to be floored
		$number = floor($number);

		// If required create a plural
		if ($number != 1)
		{
			$periods[$val] .= 's';
		}

		// Return text
		$text = sprintf("%d %s ", $number, $periods[$val]);

		// Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
		if (($val >= 1) && (($current_time - $new_time) > 0))
		{
			$text .= $this->_ago($new_time, $current_time);
		}

		return $text;
	}
561910
public function getGMTMidnightTstamp(int $tstamp)
    {
        $date = new \DateTime(date('Y-m-d', $tstamp));
        $date->setTimezone(new \DateTimeZone('GMT'));
        $date->setTime(0, 0, 0);

        return $date->getTimestamp();
    }
669736
public static function getTimeAgo($timestamp = 1, $type = self::SECOND, $time = null)
    {
        if (empty($time)) {
            $time = time();
        }
        switch ($type) {
            case self::HOUR:
                $result = $time - $timestamp * 60 * 60;
                break;
            case self::MINUTE:
                $result = $time - $timestamp * 60;
                break;
            case self::SECOND:
                $result = $time - $timestamp;
                break;
            case self::YEAR:
                $result = strtotime('-' . $timestamp . ' year', $time);
                break;
            case self::MONTH:
                $result = strtotime('-' . $timestamp . ' month', $time);
                break;
            case self::DAY:
                $result = strtotime('-' . $timestamp . ' day', $time);
                break;
            case self::WEEK:
                $result = strtotime('-' . ($timestamp * 7) . ' day', $time);
                break;
            default:
                $result = $time - $timestamp;
        }
        
        return $result;
    }
807327
public function get_prev_day($daytimestamp) {
        $date = new \DateTime();
        $date->setTimestamp($daytimestamp);
        $date->modify('-1 day');

        return $date->getTimestamp();
    }
1040137
public static function roundTimestampToYear($timestamp)
    {
        $year  = date('Y', $timestamp);
        
        $roundedTimestamp = mktime(0, 0, 0, $month=1, $day=1, $year);
        
        return $roundedTimestamp;
    }