sawyer762 sawyer762 - 1 month ago 6
PHP Question

Trim string before character in PHP

I have this date/time string

$dateTime = '2016-11-01T16:00:59:999000Z';


I would like to be able to remove the 3 digits before the
Z
.
Not really sure how to do that. I was trying to rework this:

substr($dateTime, 0, -3);


but couldn't figure out how to make it trim before the
Z
and not at the end of the string.

Answer
preg_replace("/\d{3}(Z)($)?/", "$1$2", "2016-11-01T16:00:59:999000Z");
// Result: 2016-11-01T16:00:59:999Z

Should do the job even if Z will be not in the end of the string.

Comments