473,788 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Number of days in php

realin
254 Contributor
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 1649
pbmods
5,821 Recognized Expert Expert
Heya, realin.

Expand|Select|Wrap|Line Numbers
  1. $fpart = $val - (int) $val;
  2.  
Nov 14 '07 #2
realin
254 Contributor
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 Recognized Expert Expert
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 Contributor
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
9057
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
4492
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 difference. I want it to return the difference in years, months, days, hours, minutes and seconds (a complete summary). Keeping into account of course that these are 2 real dates, I dont want it to work with 30.475 as an average number of days in a...
1
4256
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 considering converting my calculations over to business days and am wondering if there is anything built-in. I've tried searching but so far haven't seen such a thing built in. I'm thinking I'm just going to have to bite the bullet and create my own...
4
12683
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 help me with a solution. This is what I am trying to do: Pretty much, there are two dates in my database, a start date, and an end
2
3157
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 years_to_days (unsigned yr); long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);
3
12944
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 year from their year of birth, count the days in the year and compare with the days returned from timespan
9
21385
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 the number of seconds in a day? - Clint
8
7521
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
3171
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 balance if the application overlaps with the holiday. I'm using access 2000. Here's what I have so far. tblMainProfile - contains employee's profile EmployeeID <== Pkey Name Date of Birth .. tblLeaveApply - link to tblMainProfile by...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.