Search
 
SCRIPT & CODE EXAMPLE
 

PHP

today date to ago for the date in php

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
Comment

today date to ago for the date in php

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($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);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Comment

today date to ago for the date in php

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($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);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);

#Output
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Comment

today date to ago for the date in php

4 months ago
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Comment

PREVIOUS NEXT
Code Example
Php :: laravel mysql specified key was too long 
Php :: php foreach array pop 
Php :: php if short form 
Php :: laravel constract method 
Php :: laravel foreach loop index in controller 
Php :: gettype() function in PHP 
Php :: Encrypt in PHP openssl and decrypt in javascript CryptoJS 
Php :: php remove element from array by value 
Php :: php exec get pid 
Php :: php indexof 
Php :: wordpress send reset password link inside wp_new_user_notification_email 
Php :: symlink.php laravel 
Php :: substract two datetime and get the different hours and minutes php 
Php :: php one hour in the future 
Php :: create role spatie 
Php :: laravel number input positive only 
Php :: Create a table with PHP in html 
Php :: url segment in laravel 
Php :: validate columns laravel excel 
Php :: laravel collection sum 
Php :: symfony messenger 
Php :: how to debug in wordpress 
Php :: wordpress enable post thumbnail 
Php :: get node url from id twig 
Php :: continue in php 
Php :: template string php 
Php :: Remove .php extension & Remove trailing slash 
Php :: laravel where in 
Php :: php authentication 
Php :: laravel copy image with new name 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =