|
-
Jun 18th, 2002, 08:26 AM
#1
Thread Starter
Hyperactive Member
difference between 2 date
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
Last edited by prokhaled; Jun 18th, 2002 at 10:31 AM.
-
Jun 18th, 2002, 10:29 AM
#2
Stuck in the 80s
Just use the Date() and strtotime() functions:
PHP Code:
echo date("d M Y", strtotime("05-28-2002"));
the Date() function formats the date, and the strtotime() function converts the text date into a unix timestamp.
-
Jun 18th, 2002, 10:30 AM
#3
Stuck in the 80s
http://www.php.net/manual/en/function.date.php contains information on the function and many format strings that you might want to know.
-
Jun 18th, 2002, 10:33 AM
#4
Stuck in the 80s
You changed your question...
-
Jun 18th, 2002, 03:27 PM
#5
Thread Starter
Hyperactive Member
Yes
I'm Sorry.
do you have answer for my new question ??
-
Jun 18th, 2002, 11:42 PM
#6
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.
-
Jun 19th, 2002, 11:31 AM
#7
Stuck in the 80s
This should give you the difference in 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;
}
?>
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
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;
}
?>
Something like this.
-
Jun 19th, 2002, 04:55 PM
#8
Thread Starter
Hyperactive Member
V.G The Hobo
It's very good idea.
Thanks
-
Jun 19th, 2002, 07:54 PM
#9
Stuck in the 80s
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|