# Get 30 days back along with time

I want to calculate EXACT for 30 days in php from now (eg 30 Aug 14 23:06) to 30 days ago (eg 1 Aug 14 23:06). I wrote this when the current datetime goes to \$ d1 and last 30 days when the datetime goes to \$ d2, but somehow I am not getting the correct results. Any idea?

``````\$url=\$row["url"];
\$pageid=getPageID(\$url);
\$date=date('y-m-d g:i');
\$d1=strtotime(\$date);
\$d2=date(strtotime('today - 30 days'));
```

```

thank

+3

source to share

The problem is most likely caused by the wrong call `date()`

. The first argument passed to `date()`

must be a format (as shown in the Docs ) and the second must be an optional timestamp.

Try the following:

``````\$d2 = date('c', strtotime('-30 days'));
```

```

In short, the whole snippet can be simplified like this:

``````\$url = \$row["url"];
\$pageid = getPageID(\$url);
\$date = date('y-m-d g:i');
\$d1 = time();
\$d2 = date('y-m-d g:i', strtotime('-30 days'));
```

```
+5

source

You can also use the `DateTime`

class method `sub()`

along with `DateInterval`

:

``````\$now = new DateTime();
\$back = \$now->sub(DateInterval::createFromDateString('30 days'));
echo \$back->format('y-m-d g:i');
```

```
+2

source

if you want to exit enter 2014-08-01 then try below code. thank

``````\$date = '2014-08-30 23:06';
\$new_date = date('Y-m-d G:i', strtotime(\$date.' - 29 days'));
echo "30 days back is " . \$new_date;
```

```
+2

source

I know what you said with PHP, however I can't imagine not getting records from the DB. If you want to do it from DB use:

``````\$sql='SELECT * FROM myTable WHERE  date > CURRENT_DATE - INTERVAL 30 DAY';
\$pdo->query(\$sql);
```

```
+1

source

From your short description and the example you provided, I believe you want the date to be 30 days ago and the time to be the same as now. The following code will serve this purpose. Thank.

``````<?php
\$date=date('y-m-d g:i');
\$time=date('g:i');
echo "Todays date:" . \$date. "<br>";
\$d2 = date('y-m-d', strtotime('-30 days'));
echo "30 days back:" . \$d2 . ' ' .\$time;
?>
```

```
+1

source

``````Try:
echo date("Y-m-d h:i:s",strtotime('-30 days'));
```

```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7367974,"math_prob":0.8825713,"size":1825,"snap":"2021-21-2021-25","text_gpt3_token_len":523,"char_repetition_ratio":0.14442614,"word_repetition_ratio":0.0,"special_character_ratio":0.34958905,"punctuation_ratio":0.15952381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97475857,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-15T12:18:27Z\",\"WARC-Record-ID\":\"<urn:uuid:e691a72f-1ba6-4185-ad20-e763a830ce79>\",\"Content-Length\":\"44556\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:05793bb8-1527-42f9-9a17-055f54268225>\",\"WARC-Concurrent-To\":\"<urn:uuid:6b227254-ab84-4ec6-ad71-afea2f61cf1d>\",\"WARC-IP-Address\":\"52.203.36.44\",\"WARC-Target-URI\":\"https://daily-blog.netlify.app/questions/2166632/index.html\",\"WARC-Payload-Digest\":\"sha1:ZIBWIGOZK62WLJNFNYUBBR7MXCWOQNNB\",\"WARC-Block-Digest\":\"sha1:WVMHPH5NL65TUWD6ZJ3JZR24XDACL6YF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243991801.49_warc_CC-MAIN-20210515100825-20210515130825-00025.warc.gz\"}"}