...

View Full Version : difference between two dates



scroots
11-12-2003, 09:55 PM
how would i get the current date on an apache system and comapre it to another date. So i could find out if it was in the past, or how long until that date?

e.g. today (12/11/2003) and another date (15/11/2003) would show three days remaining.

i know how to do ti in javascript but would like to do it in php.


scroots

Jason
11-12-2003, 11:16 PM
in PHP there is a date command that you could use just take the current dates day, month, and year and subtract the other dates and check the result. If the result is positive then you know its in the future, negative is the past.

Jason

ConfusedOfLife
11-13-2003, 12:48 PM
You should use mktime to get your 2 timestamps and then compare them and see which one is bigger. As for as getting the difference between 2 dates, like saying it's 2 years and 3 month and 5 days difference, there is not any built in function, you can get the years difference and then do the rest by comparing month and days and ....

raf
11-13-2003, 03:42 PM
I stole some code from http://be2.php.net/date

-------
aprilchong at hotmail dot com
22-Oct-2003 09:36
This cals different days between given date and today.


function daysDiff($y1, $m1, $d1) {
$y2 = date("Y");
$m2 = date("m");
$d2 = date("d");

if (mktime(0,0,0,$m1,$d1,$y1) - mktime(0,0,0,date("m"),date("d"),date("Y"))<0) {
$diff = "expiried";
} else {
$y = $y1 - $y2;
$m = $m1 - $m2;
$d = $d1 - $d2;
$daysInMonth = date("t",$m1);
if ($d<0) {$m--;$d=$d+$daysInMonth;}
if ($m<0) {$y--;$m=$m+12;}

if ($y>0) $diff .= $y==1 ? "1 year " : "$y years ";
if ($m>0) $diff .= $m==1? "1 month and " : "$m months and ";
if ($d>0) $diff .= $d==1 ? "1 day " : "$d days ";
$diff .= " remaining";
}
return $diff;
}

April White.
----------------------
It computes the difference till today, but i suppose you can give it a twist to compute the difference between two dates.

I personally never had to do it outside SQL, which is realy easy.

scroots
11-13-2003, 09:45 PM
thanks for your code, it looks very adaptable. I will play around with it and post back.

scroots

scroots
11-13-2003, 10:32 PM
thanks people, i have come up with the code to produce how long until fom deaqdlines are.



<?php

function daysDiff($y1, $m1, $d1, $event, $link) {
$diff="";
$y2 = date("Y");
$m2 = date("m");
$d2 = date("d");

if (mktime(0,0,0,$m1,$d1,$y1) - mktime(0,0,0,date("m"),date("d"),date("Y"))>0) {

$y = $y1 - $y2;
$m = $m1 - $m2;
$d = $d1 - $d2;
$daysInMonth = date("t",$m1);
if ($d<0) {$m--;$d=$d+$daysInMonth;}
if ($m<0) {$y--;$m=$m+12;}

if ($y>0) $diff .= $y==1 ? "1 year " : "$y years ";
if ($m>0) $diff .= $m==1? "1 month and " : "$m months and ";
if ($d>0) $diff .= $d==1 ? "1 day " : "$d days ";
$diff .= " remaining ";
$diff .=$event;
$diff .=$link;
$diff .= "<br>";
}
//return $diff;
echo $diff;

}
daysDiff(2003,11,15, "chips", " <a href='#dud'>link text</a>");
daysDiff(2003,11,14, "dfgfd", " <a href='#dud'>link text</a>");
}
?>


thanks
scroots

ConfusedOfLife
11-15-2003, 11:30 AM
Originally posted by raf

I personally never had to do it outside SQL, which is realy easy.

Sorry, I know that it might look a little bit outta topic, but would you please write the SQL statement you use to get 2 dates difference?? I only know DATE_SUB that can only subtract a year from the specified year, or a month from the specified month, or a combination of 2 factors, like Y_M, but to the best of my memory it doesn't have something like Y_M_D that gives the difference.

raf
11-15-2003, 12:18 PM
but would you please write the SQL statement you use to get 2 dates difference??

That's quite impossible becuse there are bout 50 possible statements, depending on if you have a timestamp or datatime and depending on what you want the output to look like; number of days, number of months, number of years (or seconds etc)

There are numerous mySQL datatimefunctions that return datediffernces, like datediff (where you can specify the unit), period_dif (returns number of months) and of course the to_days (returns days from 01/01/0000) which you can use to first get the num of days for two dates and then substract them and proces further.

You might wanna read
http://www.mysql.com/doc/en/Date_and_time_functions.html
and specially the 'user comments' at the bottom, which contain code to compute various datedifferences, like how to compute the number of days, months and years between two dates.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum