0

I am having one weird problem.I am getting time of the day like 800 , 1200 ...

i want output String like 8:00,12:00.. I know alternatives but can anyone suggests me shortest way to complete it ..?plz

0

3 Answers 3

4

One way to do it:

echo preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);
Sign up to request clarification or add additional context in comments.

Comments

1

Another way

echo substr($time, 0, 3 === strlen($time) ? 1 : 2) . ':' . substr($time, -2);

Or you can normalize the length

$time = str_pad($time, 4, 0, STR_PAD_LEFT);

echo substr($time, 0, 2) . ':' . substr($time, 2, 2);

Comments

0

if your source is string like "800" or "1200" then solution by deceze will do :

preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);

but if it's matter of date formatting then this is the right way to do it

date("H:i");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.