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


6 answers


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'));

      

PHPFiddle




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'));

      

Click here for more details

0


source







All Articles