Is there any function to give me the difference between 2 date
for exam:
dif beteen 05-24-2002 and 05-27-2002 = 3 days
Printable View
Is there any function to give me the difference between 2 date
for exam:
dif beteen 05-24-2002 and 05-27-2002 = 3 days
Just use the Date() and strtotime() functions:
the Date() function formats the date, and the strtotime() function converts the text date into a unix timestamp.PHP Code:echo date("d M Y", strtotime("05-28-2002"));
http://www.php.net/manual/en/function.date.php contains information on the function and many format strings that you might want to know.
You changed your question...:confused:
I'm Sorry.
do you have answer for my new question ??
if you keep the date like it is and then next year you will have problems. you need to make the date like this 2002-5-24 as you can do comparisopns on dates a lot easier. the reason is that you have a date 5-24-2003 - 5-24-2002 is not 365 as far as php is concerned. but 2003-5-24 - 2002-5-24 will be don't ask me how but I have come across this.
This should give you the difference in days:
You could probably expand it farther to get weeks and years (months are too complicated and I don't feel like working it out (since each month has a different number of days)):PHP Code:<?php
$date1 = "05-24-2002";
$date2 = "05-27-2002";
echo DateDiff($date1, $date2);
function DateDiff($first, $second) {
$t1 = strtotime($first);
$t2 = strtotime($first);
$dif = $t1 - $t2;
$days = floor(($dif / (60*60*24)));
return $days;
}
?>
Something like this. :)PHP Code:<?php
function DateDiff($first, $second) {
$t1 = strtotime($first);
$t2 = strtotime($first);
$dif = $t1 - $t2;
$days = floor(($dif / (60*60*24)));
if ($days > 365) { //not leap year compliant
$years = floor($days / 365) . " years";
$days = floor($days % 365);
if ($days > 7) {
$weeks = floor($days / 7) . " weeks";
$days = floor($days % 7) . " days";
}
if ($years != 0) {
$diff = $years;
}
if ($weeks != 0) {
$diff .= ", $weeks";
}
if ($days !=0) {
$diff .= ", $days";
}
$diff .= ".";
return $diff;
}
?>
It's very good idea.
Thanks
;)