user3701398 user3701398 - 1 year ago 64
PHP Question

exploding a timestamp and adding : every 2 places

I have a PHP Script that has an INT in the format MMSSMS - Minutes Seconds Milliseconds defined as $time. I just need a : every 2 places in $time. Any help is appreciated.

Example: $message = 'I clocked in (MM:SS:MS) '.$time.';

the issue is the $time displays as 000044 instead 00:00:44 on echo

Answer Source

This code replaces 2 consecutive digits not on the end of the string with the digits followed by colon

$message = 'I clocked in (MM:SS:MS) '.preg_replace('/\d{2}(?!$)/','$0:',$time);


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