Falls mal jemand braucht (und gerne auch verbessern/erweitern möchte!):
PHP
function secondsTo_d_h_m_s($seconds,$attach='',$d=false,$h=true,$m=true,$s=true)
{
$days=0;$hours=0;$mins=0;$time=0;
$days = floor($seconds / 86400);
$hours = floor(($seconds - ($days*86400)) / 3600);
$mins = floor(($seconds - ($days*86400) - ($hours*3600)) / 60);
$sec = $seconds - ($days*86400) - ($hours*3600) - ($mins*60);
if(round($sec,0) == 60){
$mins++; $sec =0;
}
if($mins == 60){
$hours++; $mins =0;
}
if($hours == 24){
$days++; $hours =0;
}
if($d and $h and $m and $s){
$time = $days.':'.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).':'.str_pad(round($sec,0),2,'0',STR_PAD_LEFT);
}elseif($d and $h and $m and !$s){
$time = $days.':'.str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT);
}elseif($d and $h and !$m and !$s){
$time = $days.':'.str_pad($hours,2,'0',STR_PAD_LEFT);
}elseif($d and !$h and !$m and !$s){
$time = $days;
}elseif($d and !$h and $m and $s){
$time = $days.':'.str_pad($mins,2,'0',STR_PAD_LEFT).':'.str_pad(round($sec,0),2,'0',STR_PAD_LEFT);
}elseif($d and !$h and $m and !$s){
$time = $days.':'.str_pad($mins,2,'0',STR_PAD_LEFT);
}elseif($d and !$h and !$m and $s){
$time = $days.':'.str_pad(round($sec,0),2,'0',STR_PAD_LEFT);
}elseif(!$d and $h and $m and $s){
$time = str_pad(($hours+($days*24)),2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).':'.str_pad(round($sec,0),2,'0',STR_PAD_LEFT);
}elseif(!$d and $h and !$m and !$s){
$time = str_pad($hours,2,'0',STR_PAD_LEFT);
}elseif(!$d and $h and $m and !$s){
$time = str_pad(($hours+($days*24)),2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT);
}elseif(!$d and $h and !$m and $s){
$time = str_pad(($hours+($days*24)),2,'0',STR_PAD_LEFT).':'.str_pad(round($sec+($mins*60),0),2,'0',STR_PAD_LEFT);
}elseif(!$d and !$h and $m and $s){
$time = str_pad($mins+(($days*24)*60)+($hours*60),2,'0',STR_PAD_LEFT).':'.str_pad(round($sec,0),2,'0',STR_PAD_LEFT);
}elseif(!$d and !$h and $m and !$s){
$time = str_pad($mins+(($days*24)*60)+($hours*60),2,'0',STR_PAD_LEFT);
}elseif(!$d and !$h and !$m and $s){
$time = $seconds;
}
return($time.$attach);
}
function secondsTo_ago($seconds,$arr=array('d'=>array('day ago','days ago'),'h'=>array('hour ago','hours ago'),'m'=>array('minute ago','minutes ago'),'s'=>array('just now','seconds ago')))
{
if($seconds < 0){
return $seconds;
}
$days=0;$hours=0;$mins=0;$time=0;
$days = floor($seconds / 86400);
$hours = floor(($seconds - ($days*86400)) / 3600);
$mins = floor(($seconds - ($days*86400) - ($hours*3600)) / 60);
$sec = $seconds - ($days*86400) - ($hours*3600) - ($mins*60);
if(round($sec,0) == 60){
$mins++; $sec =0;
}
if($mins == 60){
$hours++; $mins =0;
}
if($hours == 24){
$days++; $hours =0;
}
if(($days+$hours+$mins) == 0){
if($sec <= 30){
$msg = $arr['s'][0];
}else{
$msg = $sec.' '.$arr['s'][1];
}
}elseif(($days+$hours) == 0 and $mins > 0){
if($mins == 1){
$msg = $mins.' '.$arr['m'][0];
}else{
$msg = $mins.' '.$arr['m'][1];
}
}elseif($days == 0 and $hours > 0){
if($hours == 1){
$msg = $hours.' '.$arr['h'][0];
}else{
$msg = $hours.' '.$arr['h'][1];
}
}elseif($days < 30){
if($days == 1){
$msg = $days.' '.$arr['d'][0];
}else{
$msg = $days.' '.$arr['d'][1];
}
}else{
$msg = date('d. F Y',(time()-$seconds));
}
return($msg);
}
echo '<b>secondsTo_d_h_m_s()</b><br />';
echo '11 sec = '.secondsTo_d_h_m_s(11,' m:s',0,0,1,1);
echo '<br />';
echo '16515 sec = '.secondsTo_d_h_m_s(16515,' h:m:s');
echo '<br />';
echo '666 sec = '.secondsTo_d_h_m_s(666,' minuten',0,0,1,1);
echo '<br />';
echo '11111 sec = '.secondsTo_d_h_m_s(11111,' stunden',0,1,0,0);
echo '<br />';
echo '123112 sec = '.secondsTo_d_h_m_s(123112,' tage',1,0,0,0);
echo '<br />';
echo '8644868 sec = '.secondsTo_d_h_m_s(8644868,' tage:stunden',1,1,0,0);
echo '<hr />';
echo '<b>secondsTo_ago()</b><br />';
echo '11 sec = '.secondsTo_ago(11);
echo '<br />';
echo '16515 sec = '.secondsTo_ago(16515);
echo '<br />';
echo '666 sec = '.secondsTo_ago(666);
echo '<br />';
echo '11111 sec = '.secondsTo_ago(11111);
echo '<br />';
echo '123112 sec = '.secondsTo_ago(123112);
echo '<br />';
echo '8644868 sec = '.secondsTo_ago(8644868);
Alles anzeigen
Output:
ZitatAlles anzeigen
secondsTo_d_h_m_s()
11 sec = 00:11 m:s
16515 sec = 04:35:15 h:m:s
666 sec = 11:06 minuten
11111 sec = 03 stunden
123112 sec = 1 tage
8644868 sec = 100:01 tage:stunden
--------------------------------------------------------------------------------
secondsTo_ago()
11 sec = just now
16515 sec = 4 hours ago
666 sec = 11 minutes ago
11111 sec = 3 hours ago
123112 sec = 1 day ago
8644868 sec = 09. February 2013
Wollte secondsTo_ago() noch erweitern - wie es bei facebook zB angezeigt wird: Freitag um 04:29
Wusste allerdings nicht auf anhieb wie ich prüfen sollte, ob date('F',...) (der Tag) noch in dieser Woche liegt.