在 PHP 中使用 str_replace() 时遇到问题

Having trouble with using str_replace() in PHP

这是我的代码:

<?php
$date_db = "2017-10-12 12:00:00";

setlocale(LC_ALL, "de_DE.UTF-8");

$date_db = strtotime($date_db);

$date_db = strftime("%e. %B %Y, %A, %k:%M Uhr", $date_db);

$date_db = str_replace(":00","",$date_db);

echo $date_db;
?>

输出为:12. Oktober 2017, Donnerstag, 12 Uhr

到目前为止一切正常。但有时没有时间,只有日期,像这样:$date_db = "2017-10-12 00:00:00";.

这将输出:12. Oktober 2017, Donnerstag, 0 Uhr.

在这种情况下,我想删除尾随 , 0 Uhr

我认为在其他 str_replace 代码行下面使用这行代码应该可以工作:$date_db = str_replace(", 0 Uhr","",$date_db);.

完整代码:

<?php
$date_db = "2017-10-12 00:00:00";

setlocale(LC_ALL, "de_DE.UTF-8");

$date_db = strtotime($date_db);

$date_db = strftime("%e. %B %Y, %A, %k:%M Uhr", $date_db);

$date_db = str_replace(":00","",$date_db);

$date_db = str_replace(", 0 Uhr","",$date_db);

echo $date_db;
?>

这应该输出12. Oktober 2017, Donnerstag,但输出是12. Oktober 2017, Donnerstag, 0 Uhr

我做错了什么?

<?php
$string = '12. Oktober 2017, Donnerstag, 0 Uhr';
$string = str_replace(", 0 Uhr", "", $string);
echo $string;
//(rtrim) Removes whitespace or other predefined characters from the right side of a string


<!---language:lang-php-->
<?php
$date_db = "2017-10-12 00:00:00";

setlocale(LC_ALL, "de_DE.UTF-8");

$date_db = strtotime($date_db);

$date_db = strftime("%e. %B %Y, %A, %k:%M Uhr", $date_db);

$date_db = str_replace(":00","",$date_db);

$date_db = rtrim($date_db ,", 0 Uhr");//(rtrim) Removes whitespace or other predefined characters from the right side of a string

echo $date_db;
?>
$date_db = "2017-10-12 10:00:00";
setlocale(LC_ALL, "de_DE.UTF-8");
$date_db = strtotime($date_db);
$date_db = strftime("%e. %B %Y, %A, %k:%M Uhr", $date_db);
$date_db = str_replace(":00","",$date_db);

//check if string contains O Uhr then only trim
if(preg_match("/ 0 Uhr/", $date_db)){
    $date_db = str_replace("0 Uhr","",$date_db);
    $date_db = rtrim($date_db, ' ,');
}
echo $date_db;