检查日期时间内的小时和分钟

时间:2014-05-22 13:18:31

标签: php datetime

如果$ 00和$分钟不是00:00,有没有办法看到这个函数内部,所以如果他们不是,我不会显示时间?

public static function datetimedutch($date)
    {
        if ($date != '')
        {
            list($newdate, $time) = explode(' ', $date);

            list($date_year, $date_month, $date_day) = explode('-', $newdate);
            list($hours, $minutes, $seconds) = explode(':', $time);

            $dag        = array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
            $maand        = array('maand','januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december');
            $weekdag    = date("w", mktime($hours, $minutes, $seconds, $date_month,$date_day,$date_year));
            $maandnr    = date("n", mktime($hours, $minutes, $seconds, $date_month,$date_day,$date_year));
            $maand_dag    = date("j", mktime($hours, $minutes, $seconds, $date_month,$date_day,$date_year));
            return $dag[$weekdag] . ' ' . $maand_dag . ' ' . $maand[$maandnr] . ' ' . $hours . ':' . $minutes;
        }
    }

2 个答案:

答案 0 :(得分:0)

这应该这样做:)

function datetimedutch($date)
{
    if (!empty($date))
    {
        $someDate = strtotime($date);
        $hours = date('H',$someDate);
        $minutes = date('i',$someDate);

        $dag        = array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
        $maand        = array('maand','januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december');
        $weekdag    = date("w", $someDate);
        $maandnr    = date("n", $someDate);
        $maand_dag    = date("j", $someDate);
        if($hours>0 || $minutes>0) {
            return $dag[$weekdag] . ' ' . $maand_dag . ' ' . $maand[$maandnr] . ' ' . $hours . ':' . $minutes;
        } else {
            return $dag[$weekdag] . ' ' . $maand_dag . ' ' . $maand[$maandnr];
        }
    } else {
        return false;
    }
}

echo datetimedutch('2014-05-15 12:39:06');
echo "<br />";
echo datetimedutch('2014-05-15 00:00:06');

答案 1 :(得分:0)

我真的建议使用自动获取语言环境格式化日期的方式,例如:

function formatDateInDutch($date = null) {

    $date = strtotime($date);
    $currentLocale = setlocale(LC_TIME, 0);

    switch (PHP_OS){
        case 'WIN':
        case 'WINNT':
            setlocale(LC_TIME, 'Dutch');
            break;

        case 'LINUX':
        default:
            setlocale(LC_TIME, 'nl_NL');

    }

    // format: http://pl1.php.net/manual/en/function.strftime.php
    $ret = strftime('%A %w %B %m', $date);

    // has minutes, display hour:minute
    if ((int)date('i', $date) != 0) {
        $ret .= ' '.date('H:i', $date);
    // has only hours
    } elseif ((int)date('H', $date) != 0 AND (int)date('i', $date) == 0) {
        $ret .= ' '.date('H', $date);
    }

    // restore default locale
    setlocale(LC_TIME, $currentLocale);

    return $ret;
}

function formatDateInDutchBetter($date = null){

    $date = new DateTime($date, new DateTimeZone('Europe/Amsterdam'));

    // format: http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
    $formatPattern = 'EEEE e LLLL dd ';

    // NOTE: $date->format and IntlDateFormatter format patterns ARE DIFFERENT!!!

    // if datetime has minutes then display hour:minutes
    if ((int)$date->format('i') != 0) {
        $formatPattern .= 'HH:mm';
    // if datetime has only minutes display minutes
    } elseif ((int)$date->format('H') != 0 AND (int)$date->format('i') == 0) {
        $formatPattern .= 'HH';
    }

    $formattedDate = IntlDateFormatter::formatObject($date, $formatPattern, 'nl_NL');

    return $formattedDate;
}

print formatDateInDutch(date('Y-m-d H:m:s'));

print '<hr>';

print formatDateInDutchBetter(date('Y-m-d H:m:s'));

我真的推荐第二种方法:使用PHP的国际化扩展。它需要启用扩展,但它与平台无关,并且很容易添加/更改其他语言环境。