Jump to content

Maths


xyn

Recommended Posts

Hey.

 

I'm trying to divide X days into weeks.

and X days into years..

 

my current code is... although i don't know the correct way.

$ago[d] = 20; #is part of an array usually.
if($ago[d]<1){
	$newdate =date("g:ia",strtotime($int));
}elseif($ago[d]>=1&&$ago[d]<7){
	$newdate =($ago[d]==1? "Yesturday.":$ago[d]." Days ago.");
}elseif($ago[d]>=7&&$ago[d]<365){
	$newdate =($ago[d]==7? "1 Week ago."$ago[d]%7)." Weeks ago.");
}elseif($ago[d]>=365){
	$newdate =($ago[d]==365? "1 Year ago."$ago[d]%365)." Years ago.");
}

Link to comment
Share on other sites

just to let you know % is the division remainder

 

for example

5%2 = 1

10%8 = 2

10%2 = 0

 

technically its called a Modulo operation in the math/programmer world (at least math higher than high school), the putman people call it that so I figure must be right.

 

The functionally still remains the same :)

Link to comment
Share on other sites

i wrote this function a while ago for something to do

<?PHP
function obtainDays($from){
   $months = 0;
   $weeks = 0;
   $days = 0;

   $now = strtotime("now");
   $difference = $now - $from ;
   
   $test = floor($difference/31557600);
   if($test > 0){
   $years = $test;
   $difference = $difference - ($years*31557600);
   }

   $test = floor($difference/2592000);
   if($test > 0){
   $months = $test;
   $difference = $difference - ($months*2592000);
   }

   $test = floor($difference/604800);
   if($test > 0){
   $weeks = $test;
   $difference = $difference - ($weeks*604800);
   }

   $test = floor($difference/86400);
   if ( $test > 0) {
   $days = $test + 1;
   $difference = $difference - ($days*86400);
   }

   $output = "that was $years Years, $months Months, $weeks Weeks, $days Days ago";

   return $output;

}

echo obtainDays(strtotime("31 august 2001")); 
//outputs      that was 6 Years, 3 Months, 2 Weeks, 2 Days ago ( dont know how accurate it is)
?>

Link to comment
Share on other sites

This is my favorite way to do it, quite similar to paul's example.

function GetTimes($seconds) {
     $years = floor($seconds % 31536000); //does not do leapyear or not...
     $seconds -= $years*31536000;
     $months = floor($seconds % 2592000); //30 days
     $seconds -= $months*2592000;
     $days = floor($seconds % 86400);
     $seconds -= $months;
     $hours = floor($seconds % 3600);
     $seconds -= $hours*3600;
     $minutes = floor($seconds % 60);
     $seconds -= $minutes*60;
     return "Years: {$years}, Months: {$months}, Days: {$days}, Hours: {$hours}, Minutes: {$minutes}, Days: {$days}";
}

 

I'm honestly not sure what you're trying to do, but that's my swing at it ;p.

 

 

(Oh, and a sidenote, yeah, I realize that function has a lot of extra math if the numbers being passed into it are like 65 everytime, but I typed that out just now, so I'm not bothering with that.  And, it might actually be faster to just divide and subtract that check....  Hmmm....  Dunno.)

Link to comment
Share on other sites

  • 1 month later...

Here's Corbin's version, but replacing the magic numbers with constants :)

No mathematical changes, just applying software engineering  practices so

that the actual mathematics is more easily understood.

 

function timeSieve($seconds)

{

 

    $SECONDS_PER_YEAR = 31536000;

$SECONDS_PER_MONTH = 2592000

 

    $years = floor($seconds % $SECONDS_PER_YEAR); //does not do leapyear or not...

    $seconds -= $years * $SECONDS_PER_YEAR;

    $months = floor($seconds % 2592000); //30 days

    $seconds -= $months*2592000;

    $days = floor($seconds % 86400);

    $seconds -= $months;

    $hours = floor($seconds % 3600);

    $seconds -= $hours*3600;

    $minutes = floor($seconds % 60);

    $seconds -= $minutes*60;

    return "Years: {$years}, Months: {$months}, Days: {$days}, Hours: {$hours}, Minutes: {$minutes}, Days: {$days}";

}

Link to comment
Share on other sites

Gee, I got caught out halfway through editing my post.

 

Here's Corbin's version, but replacing the magic numbers with constants :)

No mathematical changes, just applying software engineering  practices so

that the actual mathematics is more easily understood. It mighn't seem that

important but it's definately standard practice and makes a huge difference

in code comprehension.

 

Thanks Corbin for the code :P

 

function sieveSeconds($seconds)

{

    $SECONDS_PER_YEAR = 31536000;

    $SECONDS_PER_MOaNTH = 2592000;

    $SECONDS_PER_WEEK = 86400;

 

    // section 1:

    $years = floor($seconds % $SECONDS_PER_YEAR); //does not do leapyear or not...

    $seconds -= $years * $SECONDS_PER_YEAR;

   

    // section 2:

    $months = floor($seconds % $SECONDS_PER_YEAR); //30 days

    $seconds -= $months * $SECONDS_PER_YEAR;

 

    // section 3:

    $days = floor($seconds % $SECONDS_PER_WEEK);

    $seconds -= $months;

 

    // section 4:

    $hours = floor($seconds % 3600);

    $seconds -= $hours*3600;

 

    // section 5:

    $minutes = floor($seconds % 60);

    $seconds -= $minutes*60;

    return "Years: {$years}, Months: {$months}, Days: {$days}, Hours: {$hours}, Minutes: {$minutes}, Days: {$days}";

}

 

 

LOC: 15

Link to comment
Share on other sites

Every fourth year has 366 days. Ignore that.

Only 4 months have exactly 30 days. Ignore that.

Only 1 month has exactly four weeks (and then only 3 out of 4 years). Ignore that.

 

Math Based Programming. Are we kidding?

 

Ahh, but to know if it was leap year or not, you would have to know the actual year, not how many years based on seconds (you could assume the the first year I guess.... like every 4 years is a leap year, not x years and then 4 years is a leap year).  I guess you could also just use 364.25 for days*seconds, but that might throw some people off.

 

The months/days thing presents the same challenge as leap years.

 

Same problem (basically) again....

 

But yeah, I understand what you're saying.  The results would be entirely wrong if someone sat there and added things back up, following leap years, making the days per month fluctuate, and making the weeks per month actually correct....

 

 

 

Barand, I laughed a little when I read your reply haha.

 

Link to comment
Share on other sites

:) Well yeah :)

 

But I was under the impression that this code would only be used for displaying how long since a forum post was created. Or e.g. you have been a member for x months, y days ... and so on.

 

To just check if a person is over x amount of years would be a lot, lot more trivial wouldn't it?

 

Because you would know his birthday, and you would know today's date. Then all we need to do is to subtract $dob from $currentYear, and then you have the number of years. Then, to check if he's had his birthay this year, just compare the month and date he was born with the month and date now.

 

You would not be need to be working with seconds in the first place.

Link to comment
Share on other sites

Btw, I was kind of bored crazy so I tried to create a script that actually loops through each month using PHP's date functions to get a perfect breakdown of the years/months/days. Of course it's a dumb/crazy way to do it, but yeah it can be done. It's also too processor intensive and totally impractical in any case.

 

Or, like Corbin said fix 365 days to 365.25 days. That still leaves the fluctuating day of months.

 

Btw take a look at this link. It takes a different approach:

 

http://books.google.com/books?id=-E7_x1uW-GgC&pg=PA228&lpg=PA228&dq=php5+years+months+days&source=web&ots=nbnCicwAvc&sig=MMRb_i4g-rx9TfS2oDkEW0fv_4o#PPA229,M1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.