emopoops Posted November 18, 2009 Share Posted November 18, 2009 no its easier to just take the seconds by adding this line: $seconds = date('s', $etime); echo $seconds; or something like that i think the s takes the seconds. since etime variable is the differece wouldnt the seconds be the same? but your posts seem like u did WAYYY too much coding. for the simple task see: http://php.net/manual/en/function.time.php u probably dont have to even do the for() or whatever. i dont understand what u are doing there. the time() function seems like it can solve this problem with less code.. when combined with the date() the example 1 on the page explains what i mean. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960373 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 Your comment about using date( 's', $etime ) will not produce the desired result since $etime represents the number of seconds since the epoch.  At this point the OP has plenty of solutions to choose from. The one he chooses boils down to how precise he desires to be. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960388 Share on other sites More sharing options...
FaT3oYCG Posted November 18, 2009 Share Posted November 18, 2009 Here is some revised code that I have done which changes a couple of things.  <?php   function time_elapsed_string($previousTime)   {     /* Crate an array of all of the time values */     $timeValues = array(       (12 * (30 * (24 * (60 * 60))))  => 'year',       (30 * (24 * (60 * 60)))      => 'month',       (24 * (60 * 60))        => 'day',       (60 * 60)            => 'hour',       60                => 'minute',       1                => 'second'     );     /* Elapsed time */     $elapsedTime = time() - $previousTime;     /* Less than a second so return that */     if($elapsedTime < 1)     {       return '0 seconds ago.';     } else {       /* Create a blank output string ready for analysis */       $outputString = '';       /* For each item in the time values array */       foreach($timeValues as $seconds => $timeString)       {         /* difference is the time elapsed devided by the seconds */         $timeDifference = $elapsedTime / $seconds;         /* If the difference is larger than one */         if($timeDifference >= 1)         {           /* Round the difference */           $timeDifferenceRounded = round($timeDifference);           if($timeDifferenceRounded > 1)           {             /* Add the value to the output string with s on the end */             $outputString .= $timeDifferenceRounded . ' ' . $timeString . 's ';           } else {             /* Add the value to the output string */             $outputString .= $timeDifferenceRounded . ' ' . $timeString . ' ';           }           /* Minus the time that we just analysed from the elapsed time so that when it gets analysed */           /* for the next variable in the array it does not return something like 61 second(s) */           $elapsedTime -= date($timeDifferenceRounded * $seconds);         }       }       /* Add the ago onto the end */       $outputString .= 'ago.';       /* Return the output string. */       return $outputString;     }   } ?> <h1>Elapsed Time Function</h1> <h2>Solution for:</h2> <p>   <a href="http://www.phpfreaks.com/forums/index.php/topic,277182.msg1312173.html#msg1312173">Thread</a> </p> <!-- Simple tests of the time elapsed function. --> <h2>A couple of simple tests:</h2> <p>   <?php echo(time_elapsed_string(time() - ((60 * 60) + 1))); ?> </p> <p>   <?php echo(time_elapsed_string(time() - ((60 * 60) + 2))); ?> </p> <p>   <?php echo(time_elapsed_string(strtotime('-1 year -12 months'))); ?> </p> <p>   <?php echo(time_elapsed_string(strtotime('-1 year -2 months -3 minutes -21 seconds'))); ?> </p>  Now works perfectly, thanks to roopurt18 for pointing out the small error before.  EDIT: Forgot to put the return 0 seconds in an else statment .  EDIT 2: OMG I just broke it again . Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960418 Share on other sites More sharing options...
emopoops Posted November 18, 2009 Share Posted November 18, 2009 the time function is wayyy easier. the op should use the time() function instead and this will be solved in a snap! Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960439 Share on other sites More sharing options...
Gayner Posted November 18, 2009 Author Share Posted November 18, 2009 i do use the time function  i insert it with time, thjen i need this function to replace everything so it converts to xx seconds/minutes ago, and it does work,  but i just need it so IF it was 5minutes ago, then it will say:  5minutes 20seconds ago, or w/e instead of just 5minutes ago..  holy cow Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960441 Share on other sites More sharing options...
emopoops Posted November 18, 2009 Share Posted November 18, 2009 THEN DO IT THE WAY I FIRST POSTED NICK! Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960447 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 Some of the contributions in this thread are /facepalm Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960452 Share on other sites More sharing options...
Gayner Posted November 18, 2009 Author Share Posted November 18, 2009 Im just lost on what people are using -5days etc.  i use the function around a unixtimestamp not any lettors or -5days or w/e... it's like 12343262 is the code and i wrap function around it, maybe somepeople are lost  my server is down i'll try code when it get's up, then im leaving this server cause it's always down, i hope they can get backup cause i forgot to do a backup LOl   I mean the orignally code i posted in my 1st thread works fine, i jsut asked for a adjustment so it's not just 5minues ago it's 5minutes 20second's ago or w/e not just 5minutes.. but i guess it turned into a glamorous fulfillment of generating ideas that came from hell LOL Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960454 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 You need to read the documentation on strtotime(). Â strtotime() takes a string and converts it into a timestamp. Â Thus strtotime( '-5 days' ) returns the timestamp for exactly 5 days ago. Â strtotime( '-5 minutes -20 seconds' ) returns the timestamp for exactly 5 minutes and 20 seconds ago. Â I was using these values to test the validity and correctness of the function I wrote. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960471 Share on other sites More sharing options...
emopoops Posted November 18, 2009 Share Posted November 18, 2009 im not /facepalm. why would anyone use unix timestamp? thats rediculous! use the time() function and it will work perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960482 Share on other sites More sharing options...
roopurt18 Posted November 18, 2009 Share Posted November 18, 2009 What are you talking about?  time() returns the Unix timestamp.  How you can possibly say "don't use the unix timestamp" followed by "use a function that returns the unix timestamp?"  Or are you just being a troll? Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960484 Share on other sites More sharing options...
Gayner Posted November 19, 2009 Author Share Posted November 19, 2009 Â Â How dare u call me a troll pal. im here to learn. Â Â Yea i know but it doesn't have to be exactly 5minutes 20s econd's because if u refresh it will do 21 second's ago and so on... i use 12324513 numbers not no -5 days - 252 things to wrap my function around. WOw Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960487 Share on other sites More sharing options...
roopurt18 Posted November 19, 2009 Share Posted November 19, 2009 My troll comment was directed towards emopoops. Â Gayner, I'll repeat, strtotime() returns a timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960497 Share on other sites More sharing options...
Gayner Posted November 19, 2009 Author Share Posted November 19, 2009 My troll comment was directed towards emopoops.  Gayner, I'll repeat, strtotime() returns a timestamp.  ohlol  I get it now, lol  well my dam server is down ands i forgot to backup all my custom coded mini forum with everything..  dood i need a new server bad, plz dont remove the code u posted here i need it thx Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960499 Share on other sites More sharing options...
FaT3oYCG Posted November 19, 2009 Share Posted November 19, 2009 lol I see where I was going wrong now.  check this code out though it gives the wanted results but i dont know how you would format it so it displayed correctly.  <?php  function time_diff($prevtme)  {    $epoch = time();    $diff1 = $epoch - $prevtme;    $diff2 = time() - $diff1;    return $diff2;  }  $ret = time_diff(strtotime('-1 year -12 months'));  echo(date('d/m/Y H:i:s', $ret)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960506 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 no im not a troll and dude im sorry but unix timestamp didnt register with me with the time() function. still if that what hes using. you guys are missing out on the easiest way... Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960511 Share on other sites More sharing options...
roopurt18 Posted November 19, 2009 Share Posted November 19, 2009 Explain it to us then, in clear English please. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960525 Share on other sites More sharing options...
FaT3oYCG Posted November 19, 2009 Share Posted November 19, 2009 This works?  Could probably be shortened but just as a concept:  <?php  function timeDifference($previousTime)  {    $epoch = time();    $differenceEpochPrevious = $epoch - $previousTime;    $differencePreviousNow = time() - $differenceEpochPrevious;    $timeDifferences = array(     'year' => date('Y') - date('Y', $differencePreviousNow),     'month' => date('m') - date('m', $differencePreviousNow),     'day' => date('d') - date('d', $differencePreviousNow),     'hour' => date('H') - date('H', $differencePreviousNow),     'minuite' => date('i') - date('i', $differencePreviousNow),     'second' => date('s') - date('s', $differencePreviousNow),    );    $outputString = '';    foreach($timeDifferences as $timeString => $value)    {     if($value > 1)     {       $outputString .= $value . ' ' . $timeString . 's ';     } elseif($value > 0) {       $outputString .= $value . ' ' . $timeString . ' ';     }    }    $outputString .= 'ago.';    return $outputString;  }  echo(timeDifference(strtotime('-1 year -12 months'))); ?>  EDIT: http://gaming-network.ath.cx/time.php Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960526 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 <?php $timedifference((($etime))) = time() - $ptime; Â Â Â Â Â Â Â Â Â ///do whatever your doing to get the amount if whatever...(original post) u want the seconds of difference: $seconds = date('s', $timedifference) ?> english! Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960538 Share on other sites More sharing options...
roopurt18 Posted November 19, 2009 Share Posted November 19, 2009 That isn't even valid PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960541 Share on other sites More sharing options...
Gayner Posted November 19, 2009 Author Share Posted November 19, 2009 That isn't even valid PHP code. Â Â im loling so hard Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960543 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 the Gayner is just adding something like seconds i dont know why you guys are making it so hard. im pretty sure all that he neeeds to do is divide the etime variable by 60 and then use that one function to uh... get the remainder...(dont round) JUST FMOD() THE DARN THING! $etime = 428939; ///the number differnce $minute = 60; $seconds = fmod($etime, $minute); echo $seconds; i think ive stuck gold here folks. i was right about the main point. this is the EASIEST WAY TO ASS SECONDS. nd if u reread thats what Gayner asked. http://php.net/manual/en/function.fmod.php Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960547 Share on other sites More sharing options...
newbtophp Posted November 19, 2009 Share Posted November 19, 2009 <?php $timedifference((($etime))) = time() - $ptime; Â Â Â Â Â Â Â Â Â ///do whatever your doing to get the amount if whatever...(original post) u want the seconds of difference: $seconds = date('s', $timedifference) ?> english! Â That isn't even valid PHP code. Â Maybe he meant: Â <?php $timedifference((($etime))) = time() - $ptime; Â Â Â Â Â Â Â Â Â //seconds of the time difference $seconds = date('s', $timedifference); ?> Â Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960548 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 no actually i meant that time diference is the same as HIS ETIME vairable hence the (( )) around etime  but really guys, just fmod() it! Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960549 Share on other sites More sharing options...
Gayner Posted November 19, 2009 Author Share Posted November 19, 2009 ROFL @ EMOPOOPS this thread is /facepalmed' Â Quote Link to comment https://forums.phpfreaks.com/topic/181819-please-help-my-php-dating-function/page/2/#findComment-960552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.