Answers.Body: <p>You need to convert the <code>DateTime</code> object via <a href="https://craftcms.com/docs/3.x/dev/filters.html#date" rel="nofollow noreferrer">Filter</a>:</p>
  <pre><code> now|date('U') 
  </code></pre>
  <p>or via function</p>
  <pre><code>now.format('U')
  </code></pre>
  <p>This will format the date as a UNIX timestamp which is sufficient for querying. MySQL datetime format will work too: <code>date('Y-m-d H:i:s')</code></p>
  <p>More info can be found in the <a href="https://craftcms.com/docs/3.x/upgrade.html#currency-formatting" rel="nofollow noreferrer">upgrade documentation</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-23T19:54:29.577Z
Answers.Text: While it no longer complains that `now` is empty, after modifying `now` in my element query to `'now'|date()`, it still is saying "Object of class DateTime could not be converted to string."
  
  `$context["scriptsAboutToExpire"] = array("section" => "scripts", "order" => "expirationDate asc", "expirationDate" => array(0 => "and", 1 => (">=" . $this->extensions['craft\web\twig\Extension']->dateFilter($this->env, "now")), 2 => ("<=" . twig_date_modify_filter($this->env, $this->extensions['craft\web\twig\Extension']->dateFilter($this->env, "now"), "+2 weeks"))));`
Answers.UserDisplayName: Steven Thate
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-23T19:56:40.390Z
Answers.Text: As I said "usually formatted as a timestamp or mysql datetime" it's all stated in the docs and in my answer. You have to convert the DateTime object into a valid sql string format
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-04-23T19:30:15.720Z
Answers.LastActivityDate: 2020-11-18T16:26:47.877Z
Answers.LastEditDate: 2020-11-18T16:26:47.877Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I upgraded a Craft 2 project to Craft 3 and am working on updating some template quirks. One I am having trouble with involves the following element query:</p>
  
  <pre><code>{% set scriptsAboutToExpire = craft.entries({
      section: 'scripts',
      order: 'expirationDate asc',
      expirationDate: [ 'and', '&gt;=' ~ now, '&lt;=' ~ now|date_modify('+2 weeks') ]
  }) %}
  </code></pre>
  
  <p>This worked great in Craft 2. It grabs all <code>script</code> entries whose <code>expirationDate</code> field is two weeks away from today's date.</p>
  
  <p>However now after upgrading to Craft 3 I am getting the following error when viewing that template:</p>
  
  <p><strong>PHP Recoverable Error – yii\base\ErrorException
  Object of class DateTime could not be converted to string</strong></p>
  
  <p>with a highlight on this line:</p>
  
  <p><code>(isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this-&gt;source); })())), 2 =&gt; ("&lt;=" . twig_date_modify_filter($this-&gt;env, (isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this-&gt;source); })()), "+2 weeks"))))), "method");</code></p>
  
  <p>I tried updating all <code>.localeDate()</code> filters to <code>|date('short')</code> but that had no effect.</p>
  
Tags: <craft3><datetime>
Title: Craft 3: Error "variable now does not exist"
ThreadText: # Craft 3: Error "variable now does not exist"
  
  (Asked by: username_0 on 2018-04-23)
  
  I upgraded a Craft 2 project to Craft 3 and am working on updating some template quirks. One I am having trouble with involves the following element query:
  
  ```
  {% set scriptsAboutToExpire = craft.entries({
      section: 'scripts',
      order: 'expirationDate asc',
      expirationDate: [ 'and', '>=' ~ now, '<=' ~ now|date_modify('+2 weeks') ]
  }) %}
  
  ```
  
  This worked great in Craft 2. It grabs all `script` entries whose `expirationDate` field is two weeks away from today's date.
  
  However now after upgrading to Craft 3 I am getting the following error when viewing that template:
  
  **PHP Recoverable Error – yii\base\ErrorException
  Object of class DateTime could not be converted to string**
  
  with a highlight on this line:
  
  `(isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this->source); })())), 2 => ("<=" . twig_date_modify_filter($this->env, (isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this->source); })()), "+2 weeks"))))), "method");`
  
  I tried updating all `.localeDate()` filters to `|date('short')` but that had no effect.
  
  **Tags:** craft3, datetime
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-23. Score: 3
  
  You need to convert the `DateTime` object via [Filter](https://craftcms.com/docs/3.x/dev/filters.html#date):
  
  ```
   now|date('U') 
  
  ```
  
  or via function
  
  ```
  now.format('U')
  
  ```
  
  This will format the date as a UNIX timestamp which is sufficient for querying. MySQL datetime format will work too: `date('Y-m-d H:i:s')`
  
  More info can be found in the [upgrade documentation](https://craftcms.com/docs/3.x/upgrade.html#currency-formatting).
  
  > **Comments:**
  > **username_0**: While it no longer complains that `now` is empty, after modifying `now` in my element query to `'now'|date()`, it still is saying "Object of class DateTime could not be converted to string."  `$context["scriptsAboutToExpire"] = array("section" => "scripts", "order" => "expirationDate asc", "expirationDate" => array(0 => "and", 1 => (">=" . $this->extensions['craft\web\twig\Extension']->dateFilter($this->env, "now")), 2 => ("<=" . twig_date_modify_filter($this->env, $this->extensions['craft\web\twig\Extension']->dateFilter($this->env, "now"), "+2 weeks"))));`  
  > **username_1**: As I said "usually formatted as a timestamp or mysql datetime" it's all stated in the docs and in my answer. You have to convert the DateTime object into a valid sql string format  
  
  ---
  
  