Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_date_format($strToTimeString){
- $minute = 60;
- $hour = $minutes * 60;
- $day = $hour * 24;
- $week = $day * 7;
- $month = $week * 4;
- $year = $month * 12;
- $timeNow = strtotime("now");
- $timeDiff = $timeNow - $strToTimeString;
- if($timeDiff > $minute){
- if($timeDiff > $hour){
- if($timeDiff > $day){
- if($timeDiff > $week){
- if($timeDiff > $month){
- if($timeDiff > $year){
- // Years ago
- }
- else{
- // Months ago
- }
- }
- else{
- // Weeks ago
- }
- }
- else{
- // Days ago
- }
- }
- else
- {
- // Hours ago
- }
- }
- else{
- // Minutes ago
- }
- }
- else{
- // Seconds ago
- }
- }
- function returnInterval($date){
- $datetime1 = new DateTime($date);
- $datetime2 = new DateTime();
- $diff = $datetime1->diff($datetime2);
- $string = '';
- $pass = '';
- if($diff->y){
- $string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";
- $pass = ', ';
- }
- if($diff->m){
- $string .= $pass;
- $string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";
- $pass = ', ';
- }
- if($diff->d){
- $string .= $pass;
- $string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";
- $pass = ', ';
- }
- if($diff->h){
- $string .= $pass;
- $string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";
- $pass = ', ';
- }
- if($diff->i){
- $string .= $pass;
- $string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";
- $pass = ', ';
- }
- if($diff->s){
- $string .= $pass;
- $string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";
- }
- $pos = strrpos($string, ',');
- $string = substr_replace($string, ' and ', $pos, 2);
- return $string;
- }
- echo returnInterval('2012-09-19 13:28:45');
- // 8 days, 13 hours, 47 minutes and 44 seconds
- $now = new DateTime('now');
- $posted = new DateTime($postDateFromDBHere);
- $interval = $posted->diff($now);
- var_dump($interval);
- echo $interval->format('%y-%m-%d %h:%m:%s'); //You can do similar to format your output as you wish.
- function time_ago($date)
- {
- //echo "ss";
- if (empty($date)) {
- return "No date provided";
- }
- $periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
- $lengths = array("60","60","24","7","4.35","12","10");
- $now = time();
- $unix_date = strtotime($date);
- // check validity of date
- if (empty($unix_date)) {
- return "Bad date";
- }
- // is it future date or past date
- if ($now >= $unix_date) {
- $difference= $now - $unix_date;
- $tense = "ago";
- } else {
- $difference = $unix_date - $now;
- $tense = "from now";
- }
- for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
- $difference /= $lengths[$j];
- }
- $difference = round($difference);
- if ($difference != 1 && $j != 0) {
- $periods[$j].= "s";
- }
- if($difference!=0)
- return "$difference $periods[$j] {$tense}";
- else
- return "a few seconds ago";
- }
- function time_elapsed_since ($postedDateTime){
- $time = time() - $postedDateTime; // to get the time since that moment
- $tokens = array (
- 31536000 => 'year',
- 2592000 => 'month',
- 604800 => 'week',
- 86400 => 'day',
- 3600 => 'hour',
- 60 => 'minute',
- 1 => 'second'
- );
- foreach ($tokens as $unit => $text) {
- if ($time < $unit) continue;
- $numberOfUnits = floor($time / $unit);
- return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
- }
- }
- time_elapsed_since($postedDateTime).' ago';
- $diff = time() - $previousStamp;
- // diff is the difference between now, and when the event happened.
Add Comment
Please, Sign In to add comment