Compare 2 dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
submike
Forum Newbie
Posts: 10
Joined: Wed Sep 01, 2004 10:12 am

Compare 2 dates

Post 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
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

THIS might help you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
submike
Forum Newbie
Posts: 10
Joined: Wed Sep 01, 2004 10:12 am

Post 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?)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have the exploded date array indices turned around...
http://www.php.net/mktime
submike
Forum Newbie
Posts: 10
Joined: Wed Sep 01, 2004 10:12 am

Post 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/
Post Reply