title: How to convert localtime into YYYY-MM-DD with Perl
body: <p>I have this code which obtains the most recent coming friday, no matter what the current date is:</p>
  
  <pre><code>for(my $i = 1; $i &lt; 8; $i++) {
       my $t = time() + $i * 24 * 3600;
       next unless (localtime($t))[6] == 5; # friday                                                                                               
       $nextfri = scalar(localtime($t));                                                                                                           
       last;                                                                                                                                        
  }
  </code></pre>
  
  <p>This returns the date in the format: <code>Fri May 11 13:47:24 2018</code></p>
  
  <p>However, II must use this date for a SQL query, and so I must have this date in this format: <code>YYYY-MM-DD</code></p>
  
  <p>I have tried using Time::Piece:</p>
  
  <pre><code> print STDERR "Time::Piece: " . Time::Piece-&gt;strptime("$nextfri", '%Y %m %d'), "\n";
  </code></pre>
  
  <p>But I receive this error: <code>Error parsing time at /usr/local/lib/perl/5.18.2/Time/Piece.pm line 481, &lt;DATA&gt; line 1.</code></p>
  
  <p>How do I change the format of the date?</p>