DEV Community

Cover image for Calling all beginners - part III - fun with strings
Julien Dephix
Julien Dephix

Posted on • Updated on

Calling all beginners - part III - fun with strings

Photo by Steve Johnson on Unsplash


Hello, coders! 💻

How about a quick little challenge?

How would you left pad zeros to month and day parts of this stringified date 2022-5-3 so that we get 2022-05-03?

👇

Click for a solution using PHP
$input = '2022-5-3';
$output = vsprintf('%04d-%02d-%02d', explode('-', $input));
Enter fullscreen mode Exit fullscreen mode

vsprintf takes an array as input so we do that using explode and then we specify the output format.

More info on the official doc


Happy coding! ⌨️

Top comments (1)

Collapse
 
joolsmcfly profile image
Julien Dephix

I just added a quick solution using PHP.
Let's see your answers using other languages!