432,549 Members | 1,717 Online
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

# Calculating Number of days in php

 100+ P: 254 hi guys.. I have made a function which counts the numbers of days or hours or minutes from the current datetime to the give datetime.. but i am confused while displaying number of days along with number of hours.. rest all works fine(I Suppose). here is the code : Expand|Select|Wrap|Line Numbers   function day_counter(\$date,\$msg){ \$today=time();   \$day_to_count=strtotime(\$date); if(\$day_to_count>\$today){ \$rem=\$day_to_count-\$today; }   else {if(\$today>\$day_to_count){ \$rem=\$today-\$day_to_count;   }}   if(\$rem>=86400){ \$days=\$rem/86400;   \$days=round(\$days,2); \$dys=(int)\$days; \$h=(float)strstr(\$days,".");   \$h=round(\$h*24); if(\$h==24) \$dys+=1; if(\$h<=0 OR \$h==24) return "".\$dys." Days ".\$msg; return "".\$dys." Days ".\$h." Hours ".\$msg; } if(\$rem>=3600){ \$hours=\$rem/3600; \$hours=round(\$hours,2); \$hrs=(int)\$hours; \$mn=(float)strstr(\$hours,"."); \$mn=ceil(\$mn*60); if(\$mn<=0) return "".\$hrs." Hours ".\$msg; return "".\$hrs." Hours ".\$mn." Minutes ";  }   if(\$rem>=60){ \$min=\$rem/60; \$min=round(\$min,2); \$mnm=(int)\$min; \$sec=(float)strstr(\$min,"."); \$sec=ceil(\$sec*60); if(\$sec<=0) return "".\$mnm." Minutes ".\$msg; return "".\$mnm." Minutes ".\$sec." Seconds"; } if(\$rem>0 && \$rem<60){ return "".\$rem." Seconds".\$msg; } } And i want to know, is this the right way to get the decimal part of a number ? Nov 14 '07 #1
4 Replies

 Expert 5K+ P: 5,821 Heya, realin. Expand|Select|Wrap|Line Numbers \$fpart = \$val - (int) \$val;   Nov 14 '07 #2

 100+ P: 254 Heya, realin. Expand|Select|Wrap|Line Numbers \$fpart = \$val - (int) \$val;   hi again !! thanks for the reply.. but is the rest of code,allright ?? i mean it doesnt calculate the number of hours accurately in the first case.. that means along with the number of days.. Nov 14 '07 #3

 Expert 5K+ P: 5,821 Heya, Realin. Expand|Select|Wrap|Line Numbers \$diff = abs(time() - strtotime(\$date));   \$days = (int) (\$diff / 86400); \$day_remainder = \$diff % 86400;   \$hours = (int) (\$day_remainder / 3600); \$hour_remainder = \$day_remainder % 3600;   And so on.... I did it the long way the first time, too ~_^ Nov 14 '07 #4

 100+ P: 254 Heya, Realin. Expand|Select|Wrap|Line Numbers \$diff = abs(time() - strtotime(\$date));   \$days = (int) (\$diff / 86400); \$day_remainder = \$diff % 86400;   \$hours = (int) (\$day_remainder / 3600); \$hour_remainder = \$day_remainder % 3600;   And so on.... I did it the long way the first time, too ~_^ woah thanks.. pretty similar results.. but the code is short.. :) thanks a lot Nov 14 '07 #5