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

PHP Custom Time Ago Function

<?php
	$strTimeAgo = ""; 
	if(!empty($_POST["date-field"])) {
		$strTimeAgo = timeago($_POST["date-field"]);
	}
	function timeago($date) {
	   $timestamp = strtotime($date);	
	   
	   $strTime = array("second", "minute", "hour", "day", "month", "year");
	   $length = array("60","60","24","30","12","10");

	   $currentTime = time();
	   if($currentTime >= $timestamp) {
			$diff     = time()- $timestamp;
			for($i = 0; $diff >= $length[$i] && $i < count($length)-1; $i++) {
			$diff = $diff / $length[$i];
			}

			$diff = round($diff);
			return $diff . " " . $strTime[$i] . "(s) ago ";
	   }
	}
	
?>
Comment

PREVIOUS NEXT
Code Example
Php :: upload image to mysqli database 
Php :: PHP strcoll — Locale based string comparison 
Php :: get email with preg grep php 
Php :: laravel Call to a member function validate() on array 
Php :: Bd phone number validation in laravel 
Php :: palindrom number in php 
Php :: csv import in laravel 
Php :: PHP create array of specified size 
Php :: magento 2 add cc transportbuilder 
Php :: elvis operator php 
Php :: Laravel Http client throw exception if request is not successful 
Php :: sample test tinker php artisan 
Php :: php radian to cosine 
Php :: Include Custom Post Types Categories to the main query 
Php :: undefined offset: 7 in d:xamphtdocsphpfunctionfunction.php on line 137 
Php :: check not empty in laravel blade 
Php :: php json decode from url image 
Php :: short isset and not empty php 8 
Php :: data types of laravel migrations 
Php :: PHP catch eval output 
Php :: codeigniter set db prefix 
Php :: import data from csv laravel 
Php :: code on editing an image in database in php 
Php :: Right triangle start pattern of star 
Php :: wp retrieve acf by category name 
Php :: php default argument 
Php :: laravel get relation camelcase api 
Php :: Integrity constraint violation laravel socialite google login 
Php :: concat ternary operator 
Php :: php over serial arduino 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =