-1

如果设置为:00:00:00 ,我如何设置isClean自动删除时间。因此,如果尚未设置时间或午夜,则不要显示任何内容。

/**
 * Perform datetime formatting operations.
 * - relative: if today/yesterday
 * - clean: remove time if 00:00
 *
 * @param string $var
 * @param string||array $params
 * @return string
 */
protected function datetime($var,$params) {
    date_default_timezone_set("Europe/London");
    // 1970-01-01 00:00:00
    $template = $params;
    $isRelative = false;
    $isClean = false;
    if (is_array($params) && !empty($params['template'])) {
        if (!empty($params['relative']) && trim(strtolower($params['relative'])) == 'yes') {
            $isRelative = true;
        }
        if (!empty($params['clean']) && trim(strtolower($params['clean'])) == 'yes') {
            $isClean = true;
        }
        $template = $params['template'];
    }

    // check if its empty or not?
    if(strlen($var)<1) return $var;

    // ignore invalue request throw error?
    if(is_array($template)) return $var;

    if($var == "NOW") $var = date("d-m-Y H:i:s");

    $out = $var;
    if(strpos($out,":") === false) {
        $out = trim($out);
        $out .= " 00:00:00";
    }

    if(($timestamp = $this->getTimestamp($out)) !== false) {
        if ($isRelative) {
            $time = date('H:i', $timestamp);
            if (date('Ymd') == date('Ymd', $timestamp)) {
                // Today
                return sprintf("Today at %s", $time);
            } else {
                // Yesterday?
                $today = strtotime("12:00:00");
                $yesterday = strtotime('-1 day', $today);

                if (abs($yesterday - $timestamp) < 12*3600) {
                    return sprintf("Yesterday at %s", $time);
                }
            }
        }

        $var = date($template,$timestamp);
    }

    return $var;
}

更多信息:我们的 CMS 通过读取<match>并自动将其与数据库列名称匹配或我们手动写入来工作<format>long_datetime</format>

所以这种方法的主要原因是:

  1. 将 XML 选项与 php datetime 连接起来。
  2. 添加额外的功能,例如现在,相对日期,现在如果设置为 0,则可以清理日期时间
4

1 回答 1

1

您的要求尚不清楚,但需要澄清$varto automatically remove the time相信这会有所帮助

$xml = '
<patterns>
        <pattern>
    <match>date</match>
    <datetime>d/m/Y</datetime>
</pattern>

<pattern>
    <match>datetime</match>
    <datetime>d/m/Y H:i</datetime>
</pattern>

<pattern>
    <match>datetime_nice</match>
    <datetime>jS F Y, H:i</datetime>
</pattern>

<pattern>
    <match>datetime_iso</match>
    <datetime>c</datetime>
</pattern>

<pattern>
    <match>datetime_relative</match>
    <datetime>
        <relative>YES</relative>
        <clean>yes</clean>
        <template>jS M y, H:i</template>
    </datetime>
</pattern>

<pattern>
    <match>date_relative</match>
    <datetime>
        <relative>yes</relative>
        <template>jS M, Y</template>
    </datetime>
</pattern>

<pattern>
    <match>long_datetime</match>
    <datetime>
        <relative>YES</relative>
        <template>jS M Y \a\t H:i</template>
    </datetime>
</pattern>
</patterns>     
';

echo simpleDateTime("long_datetime",$xml);

输出

Yesterday at 11th Oct 12 
                          ^--- Date was cleaned instead of H:i in the template

使用的功能

function simpleDateTime($var, $xml) {
    $list = getList($xml);
    $list = $list[$var];
    $date = new DateTime("now", new DateTimeZone("Europe/London"));
    $output = $date->format($list['template']);
    if ($list['clean'] === true) {
        $date->setTime(0, 0, 0);
        $output = str_replace("00:00", "", $date->format($list['template']));
    }
    $output = trim($output);
    $output = rtrim($output, ",");
    if ($list['reletive'] === false) {
        return sprintf("Today at %s", $output);
    } else {
        $date->modify("+1 day");
        return sprintf("Yesterday at %s", $output);
    }
}

function getList($xml) {
    $xml = new SimpleXMLElement($xml);
    $list = array();
    foreach ( $xml->children() as $pattern ) {
        $item = array();
        $item['reletive'] = false;
        $item['clean'] = false;

        if (isset($pattern->datetime->template)) {
            $item['reletive'] = (strtoupper($pattern->datetime->relative) == "YES") ? true : false;
            $item['clean'] = (strtoupper($pattern->datetime->clean) == "YES") ? true : false;
            $item['template'] = (string) $pattern->datetime->template;
        } else {
            $item['template'] = (string) $pattern->datetime;
        }

        $list[(string) $pattern->match] = $item;
    }
    return $list;
}
于 2012-10-10T22:37:54.643 回答