Calling all beginners - part III - fun with strings
Julien Dephix

Julien Dephix @joolsmcfly

About: Been programming for over 20 years and loving it!

Location:
France
Joined:
Jun 24, 2022

Calling all beginners - part III - fun with strings

Publish Date: Jan 3 '23
1 1

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! ⌨️

Comments 1 total

  • Julien Dephix
    Julien DephixFeb 15, 2023

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

Add comment