xyn Posted December 5, 2007 Share Posted December 5, 2007 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."); } Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/ Share on other sites More sharing options...
Barand Posted December 5, 2007 Share Posted December 5, 2007 you are using % when you should be using / Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-407143 Share on other sites More sharing options...
tapos Posted December 11, 2007 Share Posted December 11, 2007 Yeah Barand is right. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-411825 Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Share Posted December 12, 2007 just to let you know % is the division remainder for example 5%2 = 1 10%8 = 2 10%2 = 0 Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-412900 Share on other sites More sharing options...
cooldude832 Posted December 14, 2007 Share Posted December 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-414610 Share on other sites More sharing options...
paul2463 Posted December 14, 2007 Share Posted December 14, 2007 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-414778 Share on other sites More sharing options...
corbin Posted December 14, 2007 Share Posted December 14, 2007 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.) Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-415144 Share on other sites More sharing options...
corbin Posted December 15, 2007 Share Posted December 15, 2007 Oh wow.... I had a slow moment.... Pretend all of those % are / in my example ;p. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-415281 Share on other sites More sharing options...
cowfish Posted February 3, 2008 Share Posted February 3, 2008 Are you trying to build a calender? Because if you are there's an easier way to do it using PHP's functions. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-456590 Share on other sites More sharing options...
cowfish Posted February 3, 2008 Share Posted February 3, 2008 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}"; } Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-456596 Share on other sites More sharing options...
cowfish Posted February 3, 2008 Share Posted February 3, 2008 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-456598 Share on other sites More sharing options...
AndyB Posted February 3, 2008 Share Posted February 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-456656 Share on other sites More sharing options...
Barand Posted February 3, 2008 Share Posted February 3, 2008 I'm sure a little nudge to the earth's orbit to make it a 400 day year, plus a simple similar nudge to the moon to give a 40 day month would be worth the effort In one go we reduce global warming and make calendar math much simpler. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-457131 Share on other sites More sharing options...
corbin Posted February 4, 2008 Share Posted February 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-457399 Share on other sites More sharing options...
cowfish Posted February 7, 2008 Share Posted February 7, 2008 How much would it be off by anyway? I doubt anybody would notice. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-460703 Share on other sites More sharing options...
Barand Posted February 7, 2008 Share Posted February 7, 2008 "Yes, Your Honour, I did sell the juvenile some Vodka because my code said he was old enough" Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-461140 Share on other sites More sharing options...
cowfish Posted February 8, 2008 Share Posted February 8, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-461593 Share on other sites More sharing options...
cowfish Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/80283-maths/#findComment-461599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.