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

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 :: mpdf output 
Php :: php replace all spaces with dashes 
Php :: laravel exists eloquent 
Php :: populate old value of dropdown laravel 
Php :: how to add an custom error to validater error in laravel 
Php :: auth.php Class "AppUser" not found 
Php :: Larvel Print last query 
Php :: laravel collection search by value 
Php :: skip add to cart for woocommerce 
Php :: php sort array by value length 
Php :: woocommerce profile photo upload 
Php :: laravel query latest 
Php :: laravel get data from this year 
Php :: laravel asset 
Php :: use model from variable laravel 
Php :: php isset array 
Php :: carbon subtract two dates 
Php :: php var_export to string 
Php :: store image in storage laravel 
Php :: Line : 83 -- syntax error, unexpected end of file php 
Php :: ajax get request in laravel 
Php :: Sending Data over another website via laravel 
Php :: php end session 
Php :: laravel curl package 
Php :: laravel group by with where clause 
Php :: laravel Your requirements could not be resolved to an installable set of packages. 
Php :: query php 
Php :: curl php 
Php :: laravel findorfail 
Php :: php array extract value 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =