Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义设置过去时间的格式_Php_Time - Fatal编程技术网

Php 自定义设置过去时间的格式

Php 自定义设置过去时间的格式,php,time,Php,Time,如果时间低于一定量,则希望显示不同的消息。我有一个我一直在做的函数,但除非时间的if为1,否则不能让它工作。我想要10分钟 function TimeSince($timestamp) { // array of time period chunks $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'),

如果时间低于一定量,则希望显示不同的消息。我有一个我一直在做的函数,但除非时间的if为1,否则不能让它工作。我想要10分钟

function TimeSince($timestamp)
    {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );

    // difference in seconds
    $since = time() - $timestamp;

    // calculate one chunk of time
    for ($i = 0, $j = count($chunks); $i < $j; $i++)
        {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0)
            {
            break;
            }
        }

    // set output var
    $output = ($count == 1) ? '1 '.$name : "$count {$name}s";

    // Displays time of if under 10 minutes displays Just Now
  if($output < 10) {
    return ("Just Now!");
  }
    else {
          return ($output . " ago");
        }   
    return $output;
    }
函数TimeSince($timestamp)
{
//时间段块数组
$chunks=数组(
阵列(60*60*24*365,'年'),
数组(60*60*24*30,'月'),
数组(60*60*24*7,'周'),
数组(60*60*24,'天'),
数组(60*60,'小时'),
数组(60,'分钟'),
);
//秒差
$since=time()-$timestamp;
//计算一段时间
对于($i=0,$j=count($chunks);$i<$j;$i++)
{
$seconds=$chunks[$i][0];
$name=$chunks[$i][1];
//查找最大的块(如果块适合,则断开)
如果(($count=floor($since/$seconds))!=0)
{
打破
}
}
//设置输出变量
$output=($count==1)?'1'。$name:“$count{$name}s”;
//显示刚刚显示的10分钟以下的if时间
如果($输出<10){
返回(“刚才!”);
}
否则{
收益($output.“ago”);
}   
返回$output;
}

您正在将
$output
设置为此行中的字符串值:

$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

然后尝试将其与一个数字进行比较。请尝试为以下代码行中的
$output

指定一个数值:

$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

重新定义<代码> $输出<代码>作为字符串,在将其赋值为“

”之前,考虑检查。
// Displays time of if under 10 minutes displays Just Now
if($count < 10) {
  return ("Just Now!");
}
  else {
  // set output var
  $output = ($count == 1) ? '1 '.$name : "$count {$name}s";
  return ($output . " ago");
}   
//显示刚刚显示的10分钟以下的if时间
如果($count<10){
返回(“刚才!”);
}
否则{
//设置输出变量
$output=($count==1)?'1'。$name:“$count{$name}s”;
收益($output.“ago”);
}   

尝试了同样的结果。您是否摆脱了$output=($count…之前设置的代码?)在IF语句中将$output更改为$count$output只是一个文本。。您不能使用它来知道它是否已过10分钟,您的$count就像一个查找块的标志。。如果要验证10分钟,您必须使用@Raoul Duke这样的$since变量。这里有一个工作示例:请接受如果有效,答案是肯定的!请注意,
$output<10
如果是0-10分钟、1-10小时、1-10天等,则为真。相反,请尝试使用类似
$since<600