Jump to content

[SOLVED] Subtracting dates to come up with a number


snowdog

Recommended Posts

I am trying to get the difference bewteen 2 dates in the number of days. ie: difference between 2006-12-8 and 2006-12-15 , The answer is 7, but just subtracting them gives me a 0 . I do not know much about strtotime and have been trying to learn but am just having difficulties with this. Here is what I have

$todays_date = date("Y-m-d"); 

  //get start and end dates for their group
  $query = "select * from group_setup WHERE member_group = $member_group"; 
  $result_setup = mysql_query($query) or die('Query failed: ' . mysql_error());
  $show_setup = mysql_fetch_object($result_setup);

  $start_date = $show_setup->start_date;
  $end_date = $show_setup->end_date;

  $date_diff = $todays_date - $start_date;
  echo $date_diff;

Thanks in advance.

Snowdog
Try rearranging your date format:

[code]
<?php
$date1 = "8 December 2006";
$date2 = "12 December 2006";

$diffDays = strtotime($date2) - strtotime($date2);
echo $diffDays;
?>
[/code]


NOTE:  This is off the top of my head and hasn't been tested.
ok got it, the strtotime converst to seconds, there are 86400 seconds in a day so you just have to divide the difference by 86400

$date_diff = (strtotime($todays_date) - strtotime($start_date))/86400;
echo $date_diff;

Thanks for putting me on the right track.

Snowdog

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.