Re: Calculating difference between two days
| From: | tg-php at gryffyndevelopment dot com | Date: | Wed, 14 Jun 2006 17:55:07 +0000 |
| Subject: | Re: Calculating difference between two days | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-237991@lists.php.net to get a copy of this message | ||
Shouldn't be too difficult in PHP.
<?php
$yesterday = date("m/d/y", mktime(0,0,0,date("m"), date("d") - 1,
date("y")));
$today = date("m/d/y");
$secondsdiff = strtotime($today) - strtotime($yesterday);
$minutesdiff = $secondsdiff / 60;
$hoursdiff = $minutesdiff / 60;
$daysdiff = $hoursdiff / 24;
echo "Seconds difference: $secondsdiff<br>\n";
echo "Minutes difference: $minutesdiff<br>\n";
echo "Hours difference: $hoursdiff<br>\n";
echo "Days difference: $daysdiff<br>\n";
?>
And if you want a larger example to play with, here's something I did a couple years ago
(forgive the word wrapping and any unrefinedness.. it's something I whipped up as a test when
someone asked a similar question back then).
There's bound to be some way to do it in SQL as well, but unless there's a specific
function for it like the other example given by another member, then you're likely to write a
lot of really ugly SQL code to tackle the problem.
Good luck!
-TG
<?php
# Standard format dates and times
$startdate = "04/12/04";
$starttime = "13:05:01";
$enddate = "10/14/04";
$endtime = "13:05:01";
# 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
$daydiff = mktime($ehour,$eminute,$esecond,$emonth,$eday,$eyear) -
mktime($shour,$sminute,$ssecond,$smonth,$sday,$syear);
if ($daydiff < 0) { $daydiff *= -1; $negative = TRUE; }
# 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
if ($negative) echo "NEGATIVE!!<br>\n";
foreach ($daydiffarr as $timeframe=>$count) {
echo "$timeframe = $count<br>\n";
}
?>
= = = Original message = = =
Hi,
I need to calculate no. of days between two dates, actually between date
stored in DB and today's date.
Does anybody has an example I can use?
I found an example on
http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html
but function gregoriantojd() doesn't work for me
Fatal error: Call to undefined function: gregoriantojd()
Found this:
Please read the manual:
http://www.php.net/manual/en/ref.calendar.php
You need to either compile the extension in or load it.
Since you provided no details about your situation at all,
I cannot help you further.
on
http://bugs.php.net/bug.php?id=10151&edit=1
Thanks on any help.
-afan
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.