Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Better time_ago Function
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2023-01-02 23:21:33

function time_ago(string $datetime, DateTime $now = null, bool $full = false, string $format = null): string
{
   try {
       $ago = new DateTime($datetime);
       if ($now === null) {
           $now = new DateTime();
       }
       $diff = $now->diff($ago);
       $diff->w = floor($diff->d / 7);
       $diff->d -= $diff->w * 7;
       $string = array(
           'y' => 'year',
           'm' => 'month',
           'w' => 'week',
           'd' => 'day',
           'h' => 'hour',
           'i' => 'minute',
           's' => 'second',
       );
       foreach ($string as $k => &$v) {
           if ($diff->$k) {
               $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
           } else {
               unset($string[$k]);
           }
       }
       if (!$full) {
           $string = array_slice($string, 0, 1);
       }
       if ($format === 'detailed') {
           return $string ? implode(', ', $string) . ' ago' : 'just now';
       } else {
           return $string ? implode(' ', $string) . ' ago' : 'just now';
       }
   } catch (Exception $e) {
       // handle exception
   }
}



// Get the current time
$now = new DateTime();

// Calculate the difference between the current time and a past date, with the default format
echo time_ago('2022-01-01'); // Outputs something like "1 year, 3 months ago"

// Calculate the difference between two past dates, with the detailed format
echo time_ago('2020-01-01', '2021-01-01', true, 'detailed'); // Outputs something like "1 year, 3 months, 2 weeks, 5 days ago"

// Calculate the difference between a past date and the current time in a different timezone
$tz = new DateTimeZone('Asia/Tokyo');
$ago = new DateTime('2022-01-01', $tz);
echo time_ago($ago, $now, false, 'detailed'); // Outputs something like "1 year, 3 months ago"
2023-01-20 01:54:56
Im testing this one. Seems reasonable and makes sense I think. I will keep it running and see
function timeAgo($time_ago) {
   $time_ago = new DateTime($time_ago);
   $now = new DateTime();
   $interval = $now->diff($time_ago);

   if ($interval->y > 0) {
       return $interval->format("%y years ago");
   } elseif ($interval->m > 0) {
       return $interval->format("%m months ago");
   } elseif ($interval->d > 0) {
       return $interval->format("%d days ago");
   } elseif ($interval->h > 0) {
       return $interval->format("%h hours ago");
   } elseif ($interval->i > 0) {
       return $interval->format("%i minutes ago");
   } else {
       return "just now";
   }
}

TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
2023-01-20 17:10:24
It gets messy when you chain methods together which DateTime doesnt support, being a static class.

For example, you cant do this

$interval = $date_time->diff($time_ago);

Which is basically:

$now->diff(this);

I think your code is the best case.
2023-01-20 21:02:09
this works better. That one above works great, just doesn't use plurals. I didnt think of that. I have 3 time functions in my code lol. I need to clean this mess up and pick one to use for crying out loud haha
function timeAgo($time_ago) {
   $time_ago = new DateTime($time_ago);
   $now = new DateTime();
   $interval = $now->diff($time_ago);

   if ($interval->y > 0) {
       $years = $interval->format("%y");
       return $years . " year" . ($years > 1 ? "s" : "") . " ago";
   } elseif ($interval->m > 0) {
       $months = $interval->format("%m");
       return $months . " month" . ($months > 1 ? "s" : "") . " ago";
   } elseif ($interval->d > 0) {
       $days = $interval->format("%d");
       return $days . " day" . ($days > 1 ? "s" : "") . " ago";
   } elseif ($interval->h > 0) {
       $hours = $interval->format("%h");
       return $hours . " hour" . ($hours > 1 ? "s" : "") . " ago";
   } elseif ($interval->i > 0) {
       $minutes = $interval->format("%i");
       return $minutes . " minute" . ($minutes > 1 ? "s" : "") . " ago";
   } else {
       return "just now";
   }
}

TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2023-01-20 21:04:29
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 5
  • Members: 0
  • Guests online: 5
  • Comments: 3
  • Messages: 1,728