简体   繁体   中英

How can I limit time ago function to under 12 months and post actual date for year or later?

I would like to post the actual date of an article (ie April 5, 2015) if it has been a year or more since its post. If less than a year, I would like to use the time ago function (ie 6 months ago).

I've tried commenting out the year in the HumanTiming Time Ago Function (posted by arnorhs) and adding an if/else condition to it

$time = strtotime('2015-04-05 17:25:43');

echo 'event happened '.humanTiming($time).' ago';

function humanTiming ($time)
{

    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
        //31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    if($time > $tokens){ 
        echo "April 5, 2015"; 
    } else {
        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
                $numberOfUnits = floor($time / $unit);
                return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
         }
     }
}

I expect the output to be "April 5, 2015", but it ignores the if/else condition and posts the date as "36 months ago".

How can I achieve the result I'm looking for?

using DateTime is way simpler

$now = new DateTime();

$then = new DateTime('April 5, 2015');

$diff = $now->diff($then); // returns DateInterval object or false on failure

if ($diff->y >= 1) {
    // show your date
    echo $then->format('Y-m-d H:i:s');
} else {
    // show the interval
    echo $diff->format('%d days ago');
}

check the documentation for more info

https://www.php.net/manual/en/class.dateinterval.php

https://www.php.net/manual/en/dateinterval.format.php

You need to check against the time of one year ago - not against the array $tokens . You can get the time of one year ago from strtotime('1 year ago') . If that is lesser than time()-$time (the current timestamp minus the $time timestamp), then the input-time is more than one year ago.

function humanTiming ($time) {
    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
        //31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    if (time()-$time < strtotime("1 year ago")) { 
        return date("F jS, Y", time()-$time);
    } else {
        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
        }
    }
}

echo 'event happened '.humanTiming($time).'';
<?php

$time = strtotime('2014-01-01 17:25:43');


function humanTiming ($time)
{
    $back_time = $time;
    $time = time() - $time; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
        //31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );
    foreach ($tokens as $unit => $text) {
        if ($time < 31536000){
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }
    }
    return date("Y-m-d H:i:s", $back_time);

}

echo 'event happened '.humanTiming($time);
?>

Everyone's different. I personally like to convert dates into unix timestamps (actual whole numbers) and work from there. Between the function and the function call are some display lines (echos) I added to give you a visual of what's happening. You can remove them, obviously.

<?php
//first here's the function
function date_less_year($entered_date_unix)
{
    $difference = time() - $entered_date_unix;
    //31536000 is a 'unix year'
    if ($difference <= 31536000)
    {
        return true; //is less than or equal to a year
    }
    else if ($difference > 31536000)
    {
        return false; //is more than a year
    }
}

/* display in the format you asked for. you can google 'php date' to learn of 
 * different formats you can use
 */
$entered_date_string = '2015-04-05 17:25:43';
$entered_date_to_display = date($entered_date_string);
echo $entered_date_to_display . ' is the date entered<br/>';
//let's turn to a unix timestampe to compare differences
$entered_date_unix = strtotime($entered_date_string);
echo $entered_date_unix . ' is the unix timestamp for entered date<br/>';
echo time() . ' is unix time for NOW <br/>';

if (date_less_year($entered_date_unix) == true)
{
    //do something for less than a year
    echo 'is less than a year!';
}
else
{
    //do something for more than a year
    echo 'is more than a year!';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM