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
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'));
source to share
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;
?>
source to share