title: PHP - create DateTime from variable
body: <p>I have variable like this:</p>
  
  <pre><code>$dateStr = "first day of January ' . (new DateTime())-&gt;format('Y') . ' 23:59:59";
  </code></pre>
  
  <p>now I want to create a <code>\DateTime</code> from this... So I'm trying like this:</p>
  
  <pre><code>$date = new \DateTime('\'' . $dateStr .'\'');
  </code></pre>
  
  <p>But it gaves error...</p>
  
  <pre><code>Exception: DateTime::__construct(): Failed to parse time string ('first day of January ' . (new DateTime())-&gt;format('Y') . ' 23:59:59') at position 0 ('): Unexpected character
  </code></pre>
  
  <p>my code:
  $dateStr = "first day of January ' . (new DateTime())->format('Y') . ' 23:59:59";</p>
  
  <p>/* works when used like this */</p>
  
  <pre><code>$dateStr = "first day of January ' . (new DateTime())-&gt;format('Y') . ' 23:59:59";
  
  $date2 = new DateTime('first day of January ' . (new DateTime())-&gt;format('Y') . ' 23:59:59');
  echo $date2-&gt;format('Y-m-d H:i:s') . PHP_EOL . PHP_EOL;
  
  /* fails when used like this */
  $date = new DateTime('\'' . $dateStr . '\'');
  echo $date-&gt;format('Y-m-d H:i:s');
  </code></pre>
  
  <p>Working fiddle: <a href="http://sandbox.onlinephpfunctions.com/code/829da64825758f35e26087b2f9a81d93421b9941" rel="nofollow">http://sandbox.onlinephpfunctions.com/code/829da64825758f35e26087b2f9a81d93421b9941</a></p>