Jump to content

Err, I suck!


fizix

Recommended Posts

Ok, I've grabbed the following snippet of code off the internet to output the amount of time elapsed since the time that was passed to it:

[code]<?php
function TimeAgo($timestamp){
// Store the current time
$current_time = time();

// Determine the difference, between the time now and the timestamp
$difference = $current_time - $timestamp;

// Set the periods of time
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");

// Set the number of seconds per period
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);

// Determine which period we should use, based on the number of seconds lapsed.
// If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
// Go from decades backwards to seconds       
for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) < 1); $val--);

// Ensure the script has found a match
if ($val < 0) $val = 0;

// Determine the minor value, to recurse through
$new_time = $current_time - ($difference % $lengths[$val]);

// Set the current value to be floored
$number = floor($number);

// If required create a plural
if($number != 1) $periods[$val].= "s";

// Return text
$text = sprintf("%d %s ", $number, $periods[$val]);   

// Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
if (($val >= 1) && (($current_time - $new_time) > 0)){
  $text .= TimeAgo($new_time);
}

return $text;
}
?> [/code]

It works really well but I'd like to change one thing. I only want it to output 2 iterations. So say I input a time that's 1 day, 1 hour, and 30 seconds from now. I only want it to tell me the 1 day and 1 hour part but to cut off the seconds. Along the same lines, if I input a time that's 1 month, 1 day, 1 hour, and 30 seconds from now, it should only output 1 month and 1 day. I hope this makes sense to everybody! Let me know if you can't understand what I'm trying to do. Thanks!
Link to comment
https://forums.phpfreaks.com/topic/31358-err-i-suck/
Share on other sites

[code]<?php
function TimeAgo($timestamp){
$current_time = time();
$difference = $current_time - $timestamp;
$periods = array("minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array(60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
for($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) < 1); $val--);
if($val < 0) $val = 0;
$new_time = $current_time - ($difference % $lengths[$val]);
$number = floor($number);
if($number != 1) $periods[$val].= "s";
$text = sprintf("%d %s ", $number, $periods[$val]);   
if(($val >= 1) && (($current_time - $new_time) > 0)) $text .= TimeAgo($new_time);
return $text;
}
echo TimeAgo('6435421');
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31358-err-i-suck/#findComment-145173
Share on other sites

[quote author=taith link=topic=119380.msg488911#msg488911 date=1166627514]
[code]<?php
function TimeAgo($timestamp){
$current_time = time();
$difference = $current_time - $timestamp;
$periods = array("minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array(60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
for($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) < 1); $val--);
if($val < 0) $val = 0;
$new_time = $current_time - ($difference % $lengths[$val]);
$number = floor($number);
if($number != 1) $periods[$val].= "s";
$text = sprintf("%d %s ", $number, $periods[$val]);   
if(($val >= 1) && (($current_time - $new_time) > 0)) $text .= TimeAgo($new_time);
return $text;
}
echo TimeAgo('6435421');
?>[/code]
[/quote]

This solution won't work. I still need to be able to see the seconds if the time entered is less that 24 hours ago. A good example would be how digg handles the time on their site.
Link to comment
https://forums.phpfreaks.com/topic/31358-err-i-suck/#findComment-145237
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.