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

The easiest and good method to claculate exact date time is TIMESTAMP.
claculate time stamp of first date and second date then subtrac them that will be your required date in the form of time TIMESTAMP cgange it to human read able form using TIMESTAMP functions

<?php 
//Our dates
$date1 = "2013-03-01 19:12:45";
$date2 = "2014-03-01 06:37:04";

//Convert them to timestamps.
$date1Timestamp = strtotime($date1);
$date2Timestamp = strtotime($date2);

//Calculate the difference.
$difference = $date2Timestamp - $date1Timestamp;

echo $difference;

?>

Reference

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.