Page 1 of 1

Compare 2 dates

Posted: Wed Sep 01, 2004 10:22 am
by submike
I'm new to php and currently stuck on what is probably an easy solution. I'm trying to check today's date with a past date and figure out how many days have passed.

Q. How many days have passed from 09-01-04 to 08-30-04?
A. 2 days

Code: Select all

$today = date("m-d-y");   //09-01-04
$file = 08-30-04; 

$diff = $today - $file;

if ($diff <= 5 days)&#123;
  do this
&#125;else&#123;
  do that
&#125;
Any help would be greatly appreciated.

Thanks in advance,
Mike

Posted: Wed Sep 01, 2004 10:24 am
by Draco_03
THIS might help you

Posted: Wed Sep 01, 2004 10:28 am
by feyd
they need to be converted to unix timestamps in order to do the math.

Code: Select all

$today = explode('-',date('Y-m-d'));
$today = mktime(0,0,0,$today[1],$today[2],$today[0]);
$then = mktime(0,0,0,$today[1]-1,30,$today[0]);

$diff = $today - $then; // difference in seconds.

Posted: Wed Sep 01, 2004 10:52 am
by submike
Thanks for the replies. I'm trying the code feyd posted right now, as I don't have time to go through the link Draco_03 sent. Thanks by the way.

Back to the code. I tried the code as follows:

Code: Select all

$today = explode('-',date('m-d-y'));
$today = mktime(0,0,0,$today&#1111;1],$today&#1111;2],$today&#1111;0]);

$val = "08-31-04";
$then = explode('-',$val);
$then = mktime(0,0,0,$then&#1111;1],$then&#1111;2],$then&#1111;0]);
$datediff = $today - $then;

if($date <= 345600)&#123; //equal to 4 days; is that right?
  echo " NEW<br>\n";
&#125;else&#123;
  echo "<br>\n";
&#125;
This code returns the following:

1231056000 09-01-04
1278226800 08-31-04
1270368000 08-28-04
1267689600 08-27-04
1259913600 08-24-04

Why is 09-01-04 less than the August dates? And how do I check that 4 days have passed (60sec x 60 min x 24hr x 4days=345600?)?

Posted: Wed Sep 01, 2004 10:59 am
by feyd
you have the exploded date array indices turned around...
http://www.php.net/mktime

Posted: Wed Sep 01, 2004 11:17 am
by submike
Thanks feyd, here's the final code that works.

Code: Select all

$today = explode('-',date('m-d-y'));
$today = mktime(0,0,0,$today&#1111;0],$today&#1111;1],$today&#1111;2]);

$val = "08-31-04";
$then = explode('-',$val);
$then = mktime(0,0,0,$then&#1111;0],$then&#1111;1],$then&#1111;2]);
$datediff = $today - $then;

if($date <= 345600)&#123;
  echo " NEW<br>\n";
&#125;else&#123;
  echo "<br>\n";
&#125;
See the final code in action at: http://www.alexandsara.com/