Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //$postedDateTime = '2012-09-23 09:11:02';
- $postedDateTime = date('2012-09-23 09:11:02');
- 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':'');
- }
- }
- echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
- ?>
- $postedDateTime = date('2012-09-23 09:11:02');
- $postedDateTime = strtotime('2012-09-23 09:11:02');
- $postedDateTime = '2012-09-23 09:11:02';
- //$postedDateTime = date('2012-09-23 09:11:02');
- function time_elapsed_since($postedDateTime){
- $time = time() - strtotime($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':'');
- }
- }
- echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
- Try This
- <?php
- //$postedDateTime = '2012-09-23 09:11:02';
- date_default_timezone_set('Asia/Kolkata');
- $postedDateTime = '2012-09-23 16:48:00';
- function time_elapsed_since($postedDateTime)
- {
- $start_date = new DateTime($postedDateTime);
- $time = $start_date->diff(new DateTime(date('Y-m-d H:i:s'))); // to get the time since that moment
- $timeDiff = null ;
- $timeDiff .= ($time->y) >= 1 ? $time->y .' years' : null;
- $timeDiff .= ($time->m) >= 1 ? $time->m .' months' : null;
- $timeDiff .= ($time->d) >= 1 ? $time->d .' days' : null;
- $timeDiff .= ($time->h) >= 1 ? $time->h .' hours' : null;
- $timeDiff .= ($time->i) >= 1 ? $time->i .' minutes' : null;
- $timeDiff .= ($time->s) >= 1 ? $time->s .' seconds' : null;
- return $timeDiff;
- }
- echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
- ?>
Add Comment
Please, Sign In to add comment