Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php time ago

// I made a dutch version of the function. just adding an 's' at the end doesn't suffice for my language
// You can probably change the names to your own language and use it as well :)
// $full has also been supported but you should probably test it first :)

public function getTimeAgo($full = false){

  $now = new DateTime;
  $ago = new DateTime($this->datetime());
  $diff = $now->diff($ago);

  $diff->w = floor($diff->d / 7);
  $diff->d -= $diff->w * 7;

  $string = array(
    'y' => 'jaren',
    'm' => 'maanden',
    'w' => 'weken',
    'd' => 'dagen',
    'h' => 'uren',
    'i' => 'minuten',
    's' => 'seconden',
  );
  $singleString = array(
    'y' => 'jaar',
    'm' => 'maand',
    'w' => 'week',
    'd' => 'dag',
    'h' => 'uur',
    'i' => 'minuut',
    's' => 'seconden',
  );
  // M.O. 2022-02-11 I rewrote this function to support dutch singles and plurals. Added some docs for next programmer to break his brain :)
  // For each possible notation, if corresponding value of current key is true (>1) otherwise remove its key/value from array
  // If the value from current key is 1, use value from $singleString array. Otherwise use value from $string array
  foreach ($string as $k => &$v) {
    if ($diff->$k) {
      if($diff->$k == 1){
        $v = $diff->$k . ' ' . $singleString[$k];
      } else {
        $v = $diff->$k . ' ' . $v;
      }
    } else {
      if($diff->$k == 1){
        unset($singleString[$k]);
      } else {
        unset($string[$k]);
      }
    }
  }

  // If $full = true, print all values.
  // Values have already been filtered with foreach removing keys that contain a 0 as value
  if (!$full) $string = array_slice($string, 0, 1);
  return $string ? implode(', ', $string) . '' : 'zojuist';
}
Comment

how to set time ago in php

function get_timeago( $ptime )
{
$etime = time() - $ptime;

if( $etime < 1 )
{
    return 'less than '.$etime.' second ago';
}

$a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
            30 * 24 * 60 * 60       =>  'month',
            24 * 60 * 60            =>  'day',
            60 * 60             =>  'hour',
            60                  =>  'minute',
            1                   =>  'second'
);

foreach( $a as $secs => $str )
{
    $d = $etime / $secs;

    if( $d >= 1 )
    {
        $r = round( $d );
        return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
    }
}
}
Comment

PREVIOUS NEXT
Code Example
Php :: DateTime and Timestamps 
Php :: guzzle http client 
Php :: grouping route in laravel 
Php :: php opendir 
Php :: laravel 8 query builder 
Php :: php camelcase to snake case 
Php :: not get child all data in relationship with parent laravel eloquent 
Php :: combine 2 columns search query laravel 
Php :: json encode 
Php :: ?? ternary operator in php 
Php :: post php 
Php :: php bcrypt password verify 
Php :: codeigniter update or create 
Php :: Remove last symbol from string 
Php :: php rand int 
Php :: wp main menu 
Php :: withdefault laravel 
Php :: replace text in string php 
Php :: php ?? 
Php :: php redirect with query string 
Php :: convert Persian/Arabic numbers to English numbers PHP 
Php :: laravel create model controller and migration on line 
Php :: how to include file in php 
Php :: include() in php 
Php :: excel date format in php 
Php :: pdf watermark dengan laravel 
Php :: how to convert youtube url to embed code in php 
Php :: update column value laravel 
Php :: search laravel 
Php :: php artisan tinker encryption cmd 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =