Re: Calculate No Of Days

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greetings!

I've posted this before, but there are always new readers so here's my script solution.  One of these days I'll make it a function or something, but this should give you a good general idea of how to do such a thing in case you need it again in the future.

btw: This also calculates hours, minutes, seconds, etc between two dates.  I start with date/times in string format just for ease of reading but you really only need them in serial date/time format ($stimestamp and $etimestamp):

<?php
# Standard format dates and times
$startdate = "10/30/02";
$starttime = "13:05:01";

$enddate = "12/30/03";
$endtime = "14:06:02";

# Break apart dates and times for mktime
list($smonth,$sday,$syear) = explode("/",$startdate);
list($emonth,$eday,$eyear) = explode("/",$enddate);
list($shour,$sminute,$ssecond) = explode(":",$starttime);
list($ehour,$eminute,$esecond) = explode(":",$endtime);

# Number of seconds in each timeframe, 1 month = 30 days
$secondsequiv = array("Years"=>31536000,
                               "Months"=>2592000,
                               "Days"=>86400,
                               "Hours"=>3600,
                               "Minutes"=>60);

# How many seconds between two dates/times
$stimestamp = mktime($shour,$sminute,$ssecond,$smonth,$sday,$syear);
$etimestamp = mktime($ehour,$eminute,$esecond,$emonth,$eday,$eyear);
$daydiff = $etimestamp - $stimestamp;

# Just to make sure I didn't use $remainder somewhere else in my script and forgot
if (isset($remainder)) unset($remainder);

# Cycle through timeframes checking to see if number is large enough to be a full year/month/day/etc
# If so, find out how many and store remainder for further processing
# If not, set to zero and continue processing
foreach ($secondsequiv as $timeframe=>$seconds) {
  if (isset($remainder)) { $checkvalue = $remainder; } else { $checkvalue = $daydiff; }
  if ($checkvalue >= $seconds) {
   $daydiffarr[$timeframe] = floor($checkvalue/$seconds);
   $remainder = $daydiff % $seconds;
  } else {
   $daydiffarr[$timeframe] = 0;
  }
}

# If $reminder is never used, then we're dealing with less than a minute's worth of time diff
if (isset($remainder)) {
  $daydiffarr["Seconds"] = $remainder;
} else {
  $daydiffarr["Seconds"] = $daydiff;
}

# Display results
foreach ($daydiffarr as $timeframe=>$count) {
  echo "$timeframe = $count<br>\n";
}
?>

= = = Original message = = =

Hi 

Is there any function avialable in PHP to calculate
the no of days by passing 2 dates like 1 argument is
1/1/2005 and the second one is 1/2/2005 then it
returns the no of days or how can i do that if there
is no builtin function .

Regards
Khuram Noman

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux