Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
One way to do it:
echo preg_replace('^(\d{1,2})(\d{2})$', '$1:$2', $time);
Add a comment
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);
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");
Required, but never shown
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.
Explore related questions
See similar questions with these tags.