Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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_Date - Fatal编程技术网

Php 时间格式的日期字符串(';时间';前)

Php 时间格式的日期字符串(';时间';前),php,date,Php,Date,我试图创建一个PHP函数,在其中输入一个$date变量,格式为:03/09/2016-12:02 我创建了一个函数,将这个日期/时间变量转换成一个字符串,表示$date变量是x天和小时前的 功能: $date = $r->date; function nicetime($date) { if(empty($date)) { return

我试图创建一个PHP函数,在其中输入一个
$date
变量,格式为:03/09/2016-12:02

我创建了一个函数,将这个日期/时间变量转换成一个字符串,表示
$date
变量是x天和小时前的

功能:

$date = $r->date;

                function nicetime($date)
                {
                    if(empty($date)) {
                        return "Geen datum gevonden.";
                    }

                    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
                    $lengths         = array("60","60","24","7","4.35","12","10");

                    ini_set('date.timezone', 'Europe/Berlin');
                    $now             = time('Y-m-d H:i:s');
                    $unix_date       = $date;

                       // check validity of date
                    if(empty($unix_date)) {    
                        return "Error.";
                    }

                    // is it future date or past date
                    if($now > $unix_date) {    
                        $difference     = $now - $unix_date;
                        $tense         = "ago";

                    } else {
                        $difference     = $unix_date - $now;
                        $tense         = "from now";
                    }

                    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                        $difference /= $lengths[$j];
                    }

                    $difference = round($difference);

                    if($difference != 1) {
                        $periods[$j].= "s";
                    }

                    return "$difference $periods[$j] {$tense}";
                }

                $postdate = nicetime($date);
$date=$r->date;
函数时间($date)
{
如果(空($日期)){
返回“Geen datum gevonden.”;
}
$periods=数组(“秒”、“分钟”、“小时”、“日”、“周”、“月”、“年”、“十年”);
$length=数组(“60”、“60”、“24”、“7”、“4.35”、“12”、“10”);
ini_集合(“日期、时区”、“欧洲/柏林”);
$now=时间('Y-m-d H:i:s');
$unix_date=$date;
//检查日期的有效性
如果(空($unix_date)){
返回“错误”;
}
//是未来日期还是过去日期
如果($now>$unix_date){
$difference=$now-$unix\u date;
$tense=“ago”;
}否则{
$difference=$unix_date-$now;
$tense=“从现在起”;
}
对于($j=0;$difference>=$length[$j]&&$j
因此,我使用
$date
插入日期。
$postdate
的结果是47年前。
有人知道我做错了什么吗?因为我不应该在47年前退休。

所以让我们把你的工作分成3个步骤: 第一步:确定时间和日期。然后使用
strotime()

步骤2:从
time()
函数获取当前
long
时间

<?php
    $date = $r->date;

        function nicetime($date)
        {
            if(empty($date)) {
                return "Geen datum gevonden.";
            }
            $r1 = split('/', $date);
            $day = $r1[0]; 
            $month = $r1[1]; 
            $year = split(' ', $r1[2]);
            $year = $year[0];
            $r1 = split('-', $date);

            $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
            $lengths         = array("60","60","24","7","4.35","12","10");

            ini_set('date.timezone', 'Europe/Berlin');

            $now             = time();
            $unix_date       = strtotime($month.'/'.$day.'/'.$year.' '. $r1[1])."</br>";;

               // check validity of date
            if(empty($unix_date)) {    
                return "Error.";
            }

            // is it future date or past date
            if($now > $unix_date) {    
                $difference     = $now - $unix_date;
                $tense         = "ago";

            } else {
                $difference     = $unix_date - $now;
                $tense         = "from now";
            }

            for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                $difference /= $lengths[$j];
            }

            $difference = round($difference);

            if($difference != 1) {
                $periods[$j].= "s";
            }

            return "$difference $periods[$j] {$tense}";
        }

        $postdate = nicetime($date); // 2 days ago
        echo $postdate;
?>

首先,我想知道为什么不使用内置的DateTime类,包括DateInterval。要做到这一点要容易得多。因为我不知道如何使用第二个:什么是$date?包含“03/09/2016-12:02”的字符串?或者时间戳?$date是一个字符串,包含“03/09/2016-12:02”,可能与“非常感谢”重复!它现在可以工作了,但是我在论坛帖子中使用它来显示它发布的时间,所以我在一个While循环中使用它,但是现在它显示最新帖子的时间,因为它不能重新声明函数。你有办法解决这个问题吗?
   if (!function_exists('nicetime')) {
    // ... proceed to declare your function
}