Farjad Hasan
Farjad Hasan

Reputation: 3364

rtrim() is trimming the string even when charlist doesn't match

I am trying to rtrim() the complete datetime whenever the time is 00:00:00. In this scenario, I just want to display date without any time.

Code:

echo rtrim('26-10-2015 06:00:00',' 00:00:00');

Result:

26-10-2015 06

I want to match the complete string to apply the right trim.

Upvotes: 0

Views: 68

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

The result you obtain is correct. The character_mask parameter help says:

You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

So, you strip the 0, : and a space from the string end.

If you still want to use your approach, use

echo rtrim(rtrim('26-10-2015 06:00:00','0..9:'));

The 0..9 "matches" all digits and the outer rtrim() will get rid of any trailing whitespace that remains after the custom rtrim.

See IDEONE demo

If you plan to change the approach, you may use strstr to obtain the substring before the first space like this:

echo strstr('26-10-2015 06:00:00',' ', true);

See demo

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

use date() and strtotime()

echo date("d-m-Y",strtotime('26-10-2015 06:00:00'));

Upvotes: 0

Related Questions