Uploader

An all around general purpose file uploader for CakePHP. Packaged as a stand alone plugin with file validation, file scanning and support for a wide range of basic mime types.

Datetime Breakdown

Breaksdown a timestamp into an array of days, months, etc since the current time.

Function: timeBreakdown()
Category: PHP
Views: 1,272
Permalink - Tinylink

/**
 * Breaksdown a timestamp into an array of days, months, etc since the current time
 * @param int $timeStamp
 * @return array
 */
function timeBreakdown($timeStamp) {
	if (!is_int($timeStamp)) $timeStamp = strtotime($timeStamp);
	$currentTime = time();
 
	$periods = array(
		'years'         => 31556926,
		'months'        => 2629743,
		'weeks'         => 604800,
		'days'          => 86400,
		'hours'         => 3600,
		'minutes'       => 60,
		'seconds'       => 1
	);
 
	$durations = array(
		'years'         => 0,
		'months'        => 0,
		'weeks'         => 0,
		'days'          => 0,
		'hours'         => 0,
		'minutes'       => 0,
		'seconds'       => 0
	);
 
	if ($timeStamp) {
		$seconds = $currentTime - $timeStamp;
 
		if ($seconds <= 0){
			return $durations;
		}
 
		foreach ($periods as $period => $seconds_in_period) {
			if ($seconds >= $seconds_in_period) {
				$durations[$period] = floor($seconds / $seconds_in_period);
				$seconds -= $durations[$period] * $seconds_in_period;
			}
		}
	}
 
	return $durations;
}
 

Return to Snippets