55 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of 55.php

  1. function time_elapsed_string($datetime, $full = false) {
  2.     $now = new DateTime;
  3.     $ago = new DateTime($datetime);
  4.  
  5.     $diff = $now->diff($ago);
  6.     $diff->w = floor($diff->d / 7);
  7.     $diff->d -= $diff->w * 7;
  8.  
  9.     $string = [
  10.         'y' => 'year',
  11.         'm' => 'month',
  12.         'w' => 'week',
  13.         'd' => 'day',
  14.         'h' => 'hour',
  15.         'i' => 'minute',
  16.         's' => 'second',
  17.     ];
  18.  
  19.     foreach ($string as $k => &$v) {
  20.         if ($diff->$k) {
  21.             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  22.         } else {
  23.             unset($string[$k]);
  24.         }
  25.     }
  26.     if (!$full) $string = array_slice($string, 0, 1);
  27.     return $string implode(', ', $string) . ' ago' : 'just now';
  28. }
  29.  
  30. echo time_elapsed_string('2017-05-05 00:22:35').PHP_EOL;
  31. echo time_elapsed_string('@1493943755').PHP_EOL; # timestamp input
  32. echo time_elapsed_string('2017-05-05 00:22:35', true).PHP_EOL;
  33.  
File Description
  • 55
  • PHP Code
  • 19 Jan-2023
  • 911 Bytes
You can Share it: