Created
April 17, 2012 02:01
-
-
Save ReneeVandervelde/2402909 to your computer and use it in GitHub Desktop.
PHP Readable Timestamps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
include "time.php"; | |
$ugly = "2012-03-16 00:12:29"; | |
$pretty = Time\readable($ugly); | |
echo $pretty; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// Maxwell Vandervelde | |
// MaxwellVandervelde.com | |
// Copyright 2012. Some Rights Reserved - Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) | |
// http://creativecommons.org/licenses/by-sa/3.0/ | |
namespace Time; | |
function getTimeDifference( $start, $end ){ | |
if($start == NULL){ | |
$start = strftime("%Y-%m-%d %H:%M:%S"); | |
} | |
$uts['start'] = strtotime( $start ); | |
$uts['end'] = strtotime( $end ); | |
if( $uts['start']!==-1 && $uts['end']!==-1 ){ | |
if( $uts['end'] >= $uts['start'] ) | |
{ | |
$diff = $uts['end'] - $uts['start']; | |
$days=intval((floor($diff/86400))); | |
$hours=intval((floor($diff/3600))); | |
$minutes=intval((floor($diff/60))); | |
if($days) | |
$diff = $diff % 86400; | |
if($hours) | |
$diff = $diff % 3600; | |
if($minutes) | |
$diff = $diff % 60; | |
$diff = intval( $diff ); | |
return( array('days'=>$days, 'hours'=>($hours), 'minutes'=>$minutes, 'seconds'=>$diff) ); | |
} else { | |
return -1; | |
} | |
} else { | |
trigger_error( "Invalid date/time data detected", E_USER_WARNING ); | |
} | |
return( false ); | |
} | |
function readable($postTime){ | |
$diff = getTimeDifference($postTime,strftime("%Y-%m-%d %H:%M:%S")); | |
if($diff['days'] == 0){ | |
if($diff['hours'] == 0){ | |
if($diff['minutes'] < 5){ | |
return " just now "; | |
} else if($diff['minutes'] == 1){ | |
return(" a minute ago "); | |
} else { | |
return(" ".$diff['minutes']." minutes ago"); | |
} | |
} else if($diff['hours'] == 1){ | |
return(" an hour ago "); | |
} else { | |
if($diff['hours'] >= 4){ | |
if(date("d",strtotime($postTime)) == date("d")) | |
return" Today at ".date("g:ia",strtotime($postTime)); | |
else | |
return" Yesterday at ".date("g:ia",strtotime($postTime)); | |
}else | |
return(" ".$diff['hours']." hours ago"); | |
} | |
}else if($diff['days'] > 365){ | |
return" on ".date("l, F jS, Y",strtotime($postTime)); | |
} else { | |
if($diff['days'] == 1) | |
return" Yesterday at ".date("g:ia",strtotime($postTime)); | |
else if($diff['days'] < 7) | |
return" on ".date("l",strtotime($postTime))." at ".date("g:ia",strtotime($postTime)); | |
else | |
return" on ".date("l, F jS",strtotime($postTime)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment