Compare Two 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
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Compare Two Dates

Post by icesolid »

I have two dates that I want to compare how many days away there is between each other.

The two date variables are $ecd and $current_date.

The $ecd is a stored date and the $current_date variable is today's date. I want the program to print out how many days it is away the $ecd is from the $current_date.

Thank you!
User avatar
chrys
Forum Contributor
Posts: 118
Joined: Tue Oct 04, 2005 9:41 am
Location: West Roxbury, MA (Boston)

Post by chrys »

Here's a ramshackle way to do it:

Code: Select all

$date1 = strtotime($current_date);
$date2 = strtotime($ecd);

$diff = $date1 - $date2;

// $diff is the difference in seconds between those two dates, so...

$days = $diff/60/24;
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

So...

Post by icesolid »

So...

How would I convert the seconds into the days?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

chrys did it for you in the last step.

X number of seconds divide by 60 seconds in a minute divide by 60 minutes in an hours divide by 24 hours in a day.

Code: Select all

<?php
// One day equals 86400 seconds
$days = $diff/86400;
?>
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Oh Yeah...

Post by icesolid »

I see, don't mind me I'm slow

Thanks chris!
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Not Working

Post by icesolid »

Here is what I get from this code.

Print Out: -20220

Code: Select all

$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);

$diff = $date1 - $date2;

$days = $diff/60/24;
echo $days . "<br>";
Help anyone?
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Multiply

Post by icesolid »

Do I need to multiply after doing the division?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try changing your first and second date based on which is bigger. This way you will always have a positive number...

Code: Select all

<?php
$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);

if ($date1 > $date2)
{
    $diff = $date1 - $date2;
}
elseif ($date2 > $date1)
{
    $diff = $date2 - $date1;
}
else
{
    $diff = 0;
}

$days = $diff/86400;
$days = number_format($days, 0, '', '');
echo $days . "<br>"; 
?>
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Still Not Working

Post by icesolid »

Using the exact same code above I get the print out below:

120

That prints out if the $row["ecd"]; date equals 02-11-2006.

Still not working, I'm curious to see if you have to multiply at all?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Change the upper code a pinch.

FROM THIS...

Code: Select all

<?php
$currentdate = date("m-d-Y");
$date1 = strtotime($currentdate);
$date2 = strtotime($row["ecd"]);
?>
TO THIS...

Code: Select all

<?php
$date1 = time();
$date2 = strtotime($row["ecd"]);
?>
Also, what format is the $row["ecd"] value?
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

ECD Format

Post by icesolid »

$row["ecd"] is in date("m-d-Y"); that format.

That is how it is stored.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

strtotime() takes a worded date, such as "Wed Feb 15, 2006 6:50 pm" and converts it to a timestamp (the number of seconds since the Epoch)

Read more about this by clicking these functions: time(), date(), strtotime()
Last edited by m3mn0n on Wed Feb 15, 2006 7:02 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So in the database, your data is stored as "m-d-Y". What is the MySQL Column data type? Have you run your query in a MySQL query browser (phpMyAdmin, Navicat, SQLYog) to see what the resulting data is from your query? Just a suggestion.

What should happen is that throwing the date value through strtotime should turn it into a unix_timestamp value as long as the date you are running through the function is 1-1-1970 and later. Then if the db date timestamp is greater than today's timestamp, today's timestamp (seconds) should be subtracted from the db date timestamp (also seconds). What is left is the difference in seconds between today and the db date. To convert it to days you need to divide the number of seconds by the number of seconds in a minute. Then divide the numbers of minutes by the number of minutes in an hour. Then divide that by the number of hours in a day. That should leave you with the difference of days. This all hinges upon your server time and the time value returned by your date data.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

HELP!!!

Post by icesolid »

The script below is my current script for comparing two dates (basically what I am trying to do). I want to rewrite this script. I am wondering if storing the dates in any other format other than ("m-d-Y") would make this process any easier? Right now the field type is just text, I was also wondering if changing the field type to date would make it easier?

Suggestions welcome!!!!

$diff - gives me the numerical value of the difference in days.
$date1 - just the numerical day of today.
$date2 - just the numerical value of the stored ECD.

PROBLEM: If the month is different like
$date1 is Januarary 1
$date2 is February 22

this program doesent work cause its simply subtracting two numbers, its not going by months, or how many days are in the month for that matter.

Here is the snippet out of my program:

Code: Select all

<?php
$date1 = strftime("%d");

list($month, $day) = explode("-", $row["ecd"]);
$date2 = $day;

if($date1 > $date2) {
    $diff = $date1 - $date2;
} elseif ($date2 > $date1) { 
    $diff = $date2 - $date1; 
} else { 
    $diff = "0";
} 

if($diff >= "7" || $diff <= "0" || $row["note"] == "false") {
    if($row["assigned"] == "false") {
        $currenttime = time();
        $futurdate = strtotime("+10 days", $currenttime);

       echo date("m-d", $futurdate);
    } elseif($row["assigned"] == "true" && $row["sent_to_reviewer"] == "false") {
        $currenttime = time();
        $futurdate = strtotime("+7 days", $currenttime);

        echo date("m-d", $futurdate);
    } elseif($row["sent_to_reviewer"] == "true") {
        $currenttime = time();
        $futurdate = strtotime("+2 days", $currenttime);

        echo date("m-d", $futurdate);
    }
} else {
    list($month, $day) = explode("-", $row["ecd"]);

    $new_date = $month . "-" . $day;

    echo $new_date;
}          
?>
Post Reply