1

This is my code:

<?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;
?>

The output is: 12. Oktober 2017, Donnerstag, 12 Uhr

This is all right so far. But sometimes there's no time, only a date, like this: $date_db = "2017-10-12 00:00:00";.

This will output: 12. Oktober 2017, Donnerstag, 0 Uhr.

In a case like this, I want to have removed the trailing , 0 Uhr.

I think it should work by using this line of code below the other str_replace code line: $date_db = str_replace(", 0 Uhr","",$date_db);.

Whole code:

<?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;
?>

This should output 12. Oktober 2017, Donnerstag, but output is 12. Oktober 2017, Donnerstag, 0 Uhr.

What am I doing wrong?

3 Answers3

2
//(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;
?>
Harsh Jain
  • 88
  • 1
  • 9
  • When using `$date_db = "2017-10-12 12:00:00";` I'm getting `12. Oktober 2017, Donnerstag, 12` instead of `12. Oktober 2017, Donnerstag, 12 Uhr`. Why that? –  Oct 19 '16 at 07:08
  • But you wanted "12. Oktober 2017, Donnerstag, 12 Uhr this one ". not this 12. Oktober 2017, Donnerstag, 12 – Harsh Jain Oct 19 '16 at 07:43
  • I don't understand, what you mean. Using this code `$date_db = "2017-10-12 12:00:00";` I want to get: `12. Oktober 2017, Donnerstag, 12 Uhr`. But with your code I'm getting only `12. Oktober 2017, Donnerstag, 12` instead. –  Oct 19 '16 at 08:42
1
$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;
Jagrati
  • 11,474
  • 9
  • 35
  • 56
  • Works. But when using `$date_db = "2017-10-12 10:00:00";` output should be `12. Oktober 2017, Donnerstag, 10 Uhr` but it is `12. Oktober 2017, Donnerstag, 1`. Why that? –  Oct 19 '16 at 07:08
  • yes , i missed it. You'll have to check if string contains "0 Uhr" then only to the last two steps. – Jagrati Oct 19 '16 at 07:13
  • Hey @Jagrati, that works, thank you very much. But, why didn't my solution work? This isn't logical for me. Can you explain that? –  Oct 19 '16 at 08:48
  • check this out it might answer to why your solution did not work. http://stackoverflow.com/questions/16563421/cant-get-str-replace-to-strip-out-spaces-in-a-php-string – Jagrati Oct 19 '16 at 09:08
0
<?php
$string = '12. Oktober 2017, Donnerstag, 0 Uhr';
$string = str_replace(", 0 Uhr", "", $string);
echo $string;
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Hi, thanks for your answer. The second code works as it is. But when using `$string = "2017-10-12 00:00:00"; setlocale(LC_ALL, "de_DE.UTF-8"); $string = strtotime($string); $string = strftime("%e. %B %Y, %A, %k:%M Uhr", $string); $string = str_replace(":00","",$string); ` instead of `$string = '12. Oktober 2017, Donnerstag, 0 Uhr';`, I'm still getting `12. Oktober 2017, Donnerstag, 0 Uhr`. –  Oct 19 '16 at 07:01
  • The first code also works as it is. But when using `$string = '12. Oktober 2017, Donnerstag, 14 Uhr';` instead of `$string = '12. Oktober 2017, Donnerstag, 0 Uhr';`, output is: `12. Oktober 2017, Donnerstag, 14`. –  Oct 19 '16 at 07:03