Jump to content

Subtract Time


SkyRanger

Recommended Posts

Try this out:

 

$now = date("Y-m-d H:i:s");

$timeago = ($now - $datetime);

$result =date("H:i:s",strtotime($timeago));

 

Which should display how many hours, min, sec ago... example: 12:15:12 (is 12 hours, 15 min, 12 seconds ago)... so there is more to do, however I hope thats a start.

 

You can contact Kevin Rose, Digg seems to have a good working model of this  ;)

 

NOTE: I did not test this, and I'm quite tired - but in my head it works - let me know.

Link to comment
https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-232881
Share on other sites

Ok, not sure if I am doing this right, actually I don't think I am doing this right because I am not getting any errors or even getting anything to display:

 

$timestamp = $rowb2["bdate"];

$difference = time() - $timestamp;

$btime = "";
       if($difference < 60)
           return $difference." seconds ago";
       else{
           $difference = round($difference / 60);
           if($difference < 60)
               return $difference." minutes ago";
           else{
               $difference = round($difference / 60);
               if($difference < 24)
                   return $difference." hours ago";
               else{
                   $difference = round($difference / 24);
                   if($difference < 7)
                       return $difference." days ago";
                   else{
                       $difference = round($difference / 7);
                       return $difference." weeks ago";
                   }
               }
           }
       }

echo $btime;

 

The entry in bdate is : 2007-04-18 22:46:13

and another is: 2007-04-17 09:06:24

 

I have myself so confused now...lol

 

Link to comment
https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233147
Share on other sites

That could be where the problem is...lol

 

page would be as:

 


function timewhen() {

..$btime = ""; code here

}

function main () {

include timewhen();

echo "Last Post was:";
echo $btime;

which I know is wrong or I wouldn't be here...lol

 

This is the first time I even attempted something like this.

Link to comment
https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233155
Share on other sites

Try something like this:

<?php
function timeago($time) {
  $min = 60;
  $hr  = 60 * $min;
  $day = 24 * $hr;

  $now  = time();
  $diff = array();

  $timediff = abs($time - $now);
  $diff['days'] = floor($timediff / $day);
  $diff['hours'] = floor(($timediff % $day) / $hr);
  $diff['minutes'] = floor((($timediff % $day) % $hr) / $min);
  $diff['seconds'] = (($timediff % $day) % $hr) % $min;

  $out = '';
  foreach ($diff as $k => $v) {
    if ($v > 0) $out .= "$v $k, ";
  }
  $out = substr($out, 0, strlen($out) - 2);
  
  return $out;
}

$date = "2007-02-14 8:15:02";
$time = strtotime($date);
echo timeago($time);
?>

Link to comment
https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233169
Share on other sites

Ok that works awesome

 

Would I do the same if I wanted it to show for example

 

$date > "2007-04-12 8:15:02";  - echo 1 week ago

or

$date > "2007-04-18 8:15:02";  - echo 1 Day Ago

or

$date > "2007-04-19 8:15:02"; - echo 3 hours 4 minutes ago

 

My function will handle any of the above scenarios, but it will count it down to the second. If it hasn't been a full day yet, it starts the count with hours.

Link to comment
https://forums.phpfreaks.com/topic/47684-subtract-time/#findComment-233272
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.