简体   繁体   中英

how to find number of days by subtracting previous date from current date

Is it possible to find the no of days between two date fields.

i want to remove the user if user does not login within 30 days. on every login login_date field will update.

i want to subtract two fields login_date and current_date and if answer is 30 or greater than 30 it will delete that user.

i am new in php and need help.. i am working on localhost.

I suggest to use DateTime and DateInterval objects.

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days "; 

read more php DateTime::diff manual

If you use timestamp format - you can use condition:

$month = 30*86400; 
if ($current_date - $login_date > $month){
    delete_user();
}

If you use datetime format - you can transform this format to timestamp with function strtotime

Following will return exact days between two dates.

 $last_login_date = "2012-04-01"; $current_date = "2012-04-30"; echo "Days: ".round(abs(strtotime($current_date)-strtotime($last_login_date))/86400); 

Hope this helps.

Hi try this function will get number of days between two dates.

function dateDiff($start, $end) {

  $start_ts = strtotime($start);

  $end_ts = strtotime($end);

  $difference = $end_ts - $start_ts;

  return round($difference / 86400);

 }

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM