sawyer762 sawyer762 - 1 year ago 112
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
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
and not at the end of the string.

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download