Help With 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

Help With Dates

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

Post by feyd »

What's the point of the code posted? What's it attempting to do?
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

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

echo $totaltimeindays;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post 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;
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

switch $today and $ecd around in the $totaltimeindays = part
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I get zero (0) as the result.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

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

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

Post 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
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

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

Post by icesolid »

Hey that's pretty handy, haven't tested it all of the way but it seems to work just fine!

Thanks!
Post Reply