Jump to content

[SOLVED] PHP Time Off


killah

Recommended Posts

Well, i was coding this little feature which will display

 

1 Day, 2 hours, 3 minutes and 4 seconds ago. If that is the unix time.

 

How ever, it displays way off. I am currently trying to display..

 

$fn->time(time());

 

$fn being the global function to my function's class.

 

This is my php code.

 

<?php //Added for the colours

function time($urtime)
{
$seconds	= floor($urtime);
$days		= intval($seconds / 1440);
$seconds  -= ($days * 1400);
$hours	= intval($seconds / 360);
$seconds  -= ($hours * 360);
$minutes	= intval($seconds / 60);
$seconds  -= ($minutes * 60);
$result		= array();
$result[]	= ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE;
$result[]	= ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE;
$result[]	= ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE;
$result[]	= ($seconds) ? sprintf('and %u second%s', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE;
return ($result == 0) ? 'Never' : implode(' ', $result).' ago';
}

 

If anyone can help figure out why it's doing this. Much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/152093-solved-php-time-off/
Share on other sites

Found this little snippet

 

function search($session_time)
{
//$session_time ="1151348975";

echo"You Entered: $session_time <BR>";
echo"The Current Time is = ".time()."<BR>";
$time_difference = time() - $session_time ;
echo"time_difference = ".$time_difference."<HR>";
$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );
echo"seconds: $seconds<br>";
echo"minutes: $minutes<br>";
echo"hours: $hours<br>";
echo"days: $days<br>";
echo"weeks: $weeks<br>";
echo"months: $months<br>";
echo"years: $years<br>";
} 

 

From: http://www.wallpaperama.com/forums/learn-php-time-function-to-display-time-hours-days-weeks-months-years-t1033.html

 

Hopefully that helps ya out.

Link to comment
https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798760
Share on other sites

Hmmm, no seem to get no where.

 

I am currently using:

 

function time($urtime)
{
	$seconds	= round(time() - $urtime);
	$minutes	= intval((time() - $urtime) / $seconds);
	$hours		= intval((time() - $urtime) / 3600);
	$days		= intval((time() - $urtime) / 86400);
	$weeks		= intval((time() - $urtime) / 604800);
	$months		= intval((time() - $urtime) / 2419200);
	$years		= intval((time() - $urtime) / 29030400);
	$result		= array();
	$result[]	= ($years) ? sprintf('%u year%s,', number_format($years), ($years > 1) ? 's' : '') : FALSE;
	$result[]	= ($months) ? sprintf('%u month%s,', number_format($months), ($months > 1) ? 's' : '') : FALSE;
	$result[]	= ($weeks) ? sprintf('%u week%s,', number_format($weeks), ($weeks > 1) ? 's' : '') : FALSE;
	$result[]	= ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE;
	$result[]	= ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE;
	$result[]	= ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE;
	$result[]	= ($seconds) ? sprintf('%s %u second%s',($minutes) ? 'and' : '', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE;
	return (count($result) == 0) ? 'Never' : implode(' ', $result).' ago';
}

 

Then displaying using:

 

echo $fn->time(1238539180);

 

It seem's to be displaying:

 

19 hours, 1 minute and 71793 seconds ago.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798886
Share on other sites

This actualy did it for me.

 

	function time($urtime)
{
	$seconds	= (((time() - $urtime) / 1) % 60);
	$minutes	= (intval((time() - $urtime) / 60) % 60); 
	$hours		= intval((time() - $urtime) / 3600);
	$days		= intval((time() - $urtime) / 86400);
	$weeks		= intval((time() - $urtime) / 604800);
	$months		= intval((time() - $urtime) / 2419200);
	$years		= intval((time() - $urtime) / 29030400);
	$result		= array();
	$result[]	= ($years) ? sprintf('%u year%s,', number_format($years), ($years > 1) ? 's' : '') : FALSE;
	$result[]	= ($months) ? sprintf('%u month%s,', number_format($months), ($months > 1) ? 's' : '') : FALSE;
	$result[]	= ($weeks) ? sprintf('%u week%s,', number_format($weeks), ($weeks > 1) ? 's' : '') : FALSE;
	$result[]	= ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE;
	$result[]	= ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE;
	$result[]	= ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE;
	$result[]	= ($seconds) ? sprintf('%s %u second%s',($minutes) ? 'and' : '', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE;
	return (count($result) == 0) ? 'Never' : implode(' ', $result).' ago';
}

 

Thank's for the help you provided.

Link to comment
https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798927
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.