473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Calculating Number of days in php

realin
254 100+
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
4 1635
pbmods
5,821 Expert 4TB
Heya, realin.

Expand|Select|Wrap|Line Numbers
  1. $fpart = $val - (int) $val;
  2.  
Nov 14 '07 #2
realin
254 100+
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
5,821 Expert 4TB
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
254 100+
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

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

Similar topics

6
by: Ralph Freshour | last post by:
What's a good way to calculate the number of days between two dates in the following format: 2003-07-15 2003-08-02 I've looked at the PHP date functions but I'm still a bit lost...
4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
1
by: David Stockwell | last post by:
I''m wondering if the Calendar object has an option to only do calculations based on business days (ie M-F). Additionally does it have a way to get holidays into itself? Currently I'm...
4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
2
by: celsius | last post by:
Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include <stdio.h> int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long...
3
by: Ron Vecchi | last post by:
I need to calculate the age of a person based on a DateTime BirthDate I was thinking TimeSpan ts = DateTime.Now - BirthDate; //I can get the days but not years. // I could check each...
9
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
5
by: jjkeeper | last post by:
G'day, I'm currently working on an annual leave database for the company, so far so good, till they want the database to be able to identify public holidays and prevent reducing the employee's...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.