PHP time formatting ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I would like to do something like this:

if my user enters:
into a text box, when my PHP executes and displays this data on the
next page it will show up as:
so basically it will already have the : in it. How would I do this?


Re: PHP time formatting ? escribió:
Quoted text here. Click to load it


function time_format($string){
    if(preg_match('/^(\d)([0-5][0-9])$/', $string, $matches)){
        if($matches[1]>=0 && $matches[1]<=23){
            return $matches[1] . ':' . $matches[2];

    return $string;

echo time_format('1220') . "\n";
echo time_format('120') . "\n";
echo time_format('330') . "\n";
echo time_format('9999') . "\n";
echo time_format('2500') . "\n";
echo time_format('865') . "\n";
echo time_format('foo') . "\n";

If you don't need/want validation, then you can probably do it with

You could also add trim() somewhere in your code in order to remove
leading or trailing spaces.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: PHP time formatting ?

On 15 May, 16:33, "=C1lvaro G. Vicario"
Quoted text here. Click to load it

A better solution would be to do it in Javascript - in the onblur
method - that way you keep the rewrite closer to the user.


Site Timeline