Page 1 of 1

Help With Dates

Posted: Fri Feb 24, 2006 1:44 pm
by icesolid
The script below is my current script for comparing two dates (also shows what I am doing after the compare). 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? I was also wondering if changing the field type to date would make it easier? Right now the field type is text. I thought maybe using the TIMESTAMP or DATE field type would be better?

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 dates go like this:
$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;
}          
?>

Posted: Fri Feb 24, 2006 2:05 pm
by feyd
What's the point of the code posted? What's it attempting to do?

Posted: Fri Feb 24, 2006 2:46 pm
by icesolid
It is attempting to compare two dates to find the difference in days between them.

What I am trying to do is find out if $today (today's date) is 7 days until $ecd (estimated completion date).

But using the code I have below if $today is January 1 and the $ecd is February 3 it gives me off numbers because the code below does not consider the month or how many days are in the month, it just subtracts two numbers (the day stored in $today variable and the day stored in the $ecd variable).

I have tried playing with the strtotime(); function, but I am unable to figure anything out.

Because using the strtotime(); function you can specify 1 week, 7 days or whatever from a date.

Posted: Fri Feb 24, 2006 2:58 pm
by s.dot

Code: Select all

$totaltimeindays = (strtotime($seconddate)-strtotime($firstdate))*60*60*24;

echo $totaltimeindays;

Posted: Fri Feb 24, 2006 3:01 pm
by icesolid
Using the code you just posted I get this result:

01-01-1970-679311360000

That result shows up if:
$today = 02-24-2006
$ecd = 02-27-2006

Here is the code im using:

Code: Select all

<?php
$today = "02-24-2006";
$ecd = "02-27-2006";

$totaltimeindays = (strtotime($today)-strtotime($ecd))*60*60*24; 

echo $totaltimeindays;
?>

Posted: Fri Feb 24, 2006 3:04 pm
by s.dot
switch $today and $ecd around in the $totaltimeindays = part

Posted: Fri Feb 24, 2006 3:25 pm
by icesolid
I get zero (0) as the result.

Posted: Fri Feb 24, 2006 3:43 pm
by icesolid
Like I was asking in my first question, if I made my field types DATETIME or TIMESTAMP is their a way of comparing the two dates using strtotime(); Right now my field type for $ecd is TEXT.

It seems like there would be but I am just not sure how.

Posted: Fri Feb 24, 2006 3:45 pm
by feyd
there's a Code Snippet that can tell you the difference in various units.
viewtopic.php?t=29341

Just need to properly get the difference in seconds between the two dates.

If you stored the dates in Y-m-d format like MySQL normally would, it could convert them to unix timestamps for you, but since you aren't

Code: Select all

$date = explode('-',$date);
$date = mktime(0,0,0,$date[0],$date[1],$date[2]);

Posted: Fri Feb 24, 2006 5:05 pm
by icesolid
feyd could you produce a little bit of code for me so I can see what it would look like, I have been playing with code but nothing is working. I will convert the way I store my dates to Y-m-d, so you can show me a way to accomplish this using the proper format.

Thanks

Posted: Fri Feb 24, 2006 5:21 pm
by SKDevelopment

Code: Select all

<?php
$today = "02-24-2006"; 
$ecd = "02-27-2006"; 

$a = explode('-',$today); 
$date1 = mktime(0,0,0,$a[0],$a[1],$a[2]);

$a = explode('-',$ecd); 
$date2 = mktime(0,0,0,$a[0],$a[1],$a[2]);

$totaltimeindays = abs($date1-$date2)/60/60/24;  

echo $totaltimeindays; 
?>
--
Best Regards,
Sergey Korolev
www.SKDevelopment.com

Posted: Fri Feb 24, 2006 6:00 pm
by icesolid
Hey that's pretty handy, haven't tested it all of the way but it seems to work just fine!

Thanks!