Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get 30 days back date along with time

I want to calculate EXACT past 30 days time period in php from now (for example 30 aug 14 23:06) to 30 days back (for example 1 aug 14 23:06). I wrote this where current datetime goes in $d1 and past 30 days datetime goes in $d2 but somehow i am not getting 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'));

Thanks

like image 989
Mumbai CabinCrew Avatar asked Sep 25 '14 17:09

Mumbai CabinCrew


1 Answers

The problem is likely caused by the malformed date() call. The first argument passed to date() should be the format (as shown in the Docs) and the second should be an optional timestamp.

Try this:

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

PHPFiddle


As a short aside, the whole snippet can be simplified as follows:

$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'));
like image 169
BenM Avatar answered Sep 20 '22 05:09

BenM