By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
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

realin
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
  1.  
  2. function day_counter($date,$msg){
  3. $today=time();
  4.  
  5. $day_to_count=strtotime($date);
  6. if($day_to_count>$today){
  7. $rem=$day_to_count-$today;
  8. }
  9.  
  10. else {if($today>$day_to_count){
  11. $rem=$today-$day_to_count;
  12.  
  13. }}
  14.  
  15. if($rem>=86400){
  16. $days=$rem/86400;
  17.  
  18. $days=round($days,2);
  19. $dys=(int)$days;
  20. $h=(float)strstr($days,".");
  21.  
  22. $h=round($h*24);
  23. if($h==24)
  24. $dys+=1;
  25. if($h<=0 OR $h==24)
  26. return "<b>".$dys."</b> Days ".$msg;
  27. return "<b>".$dys."</b> Days <b>".$h."</b> Hours ".$msg;
  28. }
  29. if($rem>=3600){
  30. $hours=$rem/3600;
  31. $hours=round($hours,2);
  32. $hrs=(int)$hours;
  33. $mn=(float)strstr($hours,".");
  34. $mn=ceil($mn*60);
  35. if($mn<=0)
  36. return "<b>".$hrs."</b> Hours ".$msg;
  37. return "<b>".$hrs."</b> Hours <b>".$mn."</b> Minutes ";
  38.  }
  39.  
  40. if($rem>=60){
  41. $min=$rem/60;
  42. $min=round($min,2);
  43. $mnm=(int)$min;
  44. $sec=(float)strstr($min,".");
  45. $sec=ceil($sec*60);
  46. if($sec<=0)
  47. return "<b>".$mnm."</b> Minutes ".$msg;
  48. return "<b>".$mnm."</b> Minutes <b>".$sec." </b>Seconds";
  49. }
  50. if($rem>0 && $rem<60){
  51. return "<b>".$rem."</b> Seconds".$msg;
  52. }
  53. }
And i want to know, is this the right way to get the decimal part of a number ?
Nov 14 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
Heya, realin.

Expand|Select|Wrap|Line Numbers
  1. $fpart = $val - (int) $val;
  2.  
Nov 14 '07 #2

realin
100+
P: 254
Heya, realin.

Expand|Select|Wrap|Line Numbers
  1. $fpart = $val - (int) $val;
  2.  
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

pbmods
Expert 5K+
P: 5,821
Heya, Realin.

Expand|Select|Wrap|Line Numbers
  1. $diff = abs(time() - strtotime($date));
  2.  
  3. $days = (int) ($diff / 86400);
  4. $day_remainder = $diff % 86400;
  5.  
  6. $hours = (int) ($day_remainder / 3600);
  7. $hour_remainder = $day_remainder % 3600;
  8.  
And so on....

I did it the long way the first time, too ~_^
Nov 14 '07 #4

realin
100+
P: 254
Heya, Realin.

Expand|Select|Wrap|Line Numbers
  1. $diff = abs(time() - strtotime($date));
  2.  
  3. $days = (int) ($diff / 86400);
  4. $day_remainder = $diff % 86400;
  5.  
  6. $hours = (int) ($day_remainder / 3600);
  7. $hour_remainder = $day_remainder % 3600;
  8.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.