Jump to content

Please Help my PHP Dating Function.


Gayner

Recommended Posts

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.

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

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.

Link to comment
Share on other sites

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 :P .

 

EDIT 2: OMG I just broke it again :P.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

 

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

Link to comment
Share on other sites

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 :P thx

Link to comment
Share on other sites

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));
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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);

?>

 

:P

Link to comment
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.