Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Help On Calc Date Issues !
magiccode9
post Jun 22 2008, 08:26 AM
Post #1


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 48
Joined: 7-November 05
Member No.: 9,489



Hi, all


I need going to write a small function that calc some interval from day to day
and to display it base on their size.

For example, if the value was 1 minute or over that it display as x min.
or if it hours then display it as x hours and so on.

Let's say from a day range that has been passed 600000 second, than
I have to change it to some mins or hours or days and so on....

I just get start with use the number of seconds and divide it with 60 and converted to mins,
and go on up to days. But don't know how to get the next steps on.

Any suggest are appreciate.


Thanks,

Regards,
Eric,
Go to the top of the page
 
+Quote Post
faulty.lee
post Jun 22 2008, 04:56 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 474
Joined: 5-November 06
Member No.: 17,016



You need to be a bit more specific here. It'll be helpful if you can answer a few of the question below before anyone can lend a hand.

1. What programming language do you intent to use for your "function"? I suppose you're going to implement this into your existing program

2. You only need 1 type of unit? If it's in the hours, then no need to display minutes?

3. If (2) is yes, then do you need to round up the figure?
a. 1.52 Hours OR b. 2 Hours

4. If (2) is no, then what is your least significant unit?
a. 1 Hours 30 Mins 50 Secs OR b. 1 Hours 31 Mins OR c. 2 Hours ..... etc
Go to the top of the page
 
+Quote Post
magiccode9
post Jun 22 2008, 06:49 PM
Post #3


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 48
Joined: 7-November 05
Member No.: 9,489



Hi, faulty.lee

Hmm, I don't specfic the language to be used due it's language independently. smile.gif
But it is actually used with php.

To summary up all the questions metioned above.
Yes, I do also thinking these all issues ....

Actually, I wish to something like viewvc.
That is a source code viewer that shows all aspect of repository information,
including a file's from it's first checkin to it's most current version have been passed.

For simply the question, I have choosed that round the time period to integer.

Do I need to do somethink like divide the number of days with 365 if it is > than it.

if it did, then I have number of years + a number of days within a year.

But because each month have different days, I have loop it all back from now - number of year calc above and plus these all.

Althrough it much like works. But still seems missing somethings.

Thanks,

Eric,


Go to the top of the page
 
+Quote Post
faulty.lee
post Jun 23 2008, 08:33 AM
Post #4


Premium Member
Group Icon

Group: [HOSTED]
Posts: 474
Joined: 5-November 06
Member No.: 17,016



Alright, this is going to be a long one.

CODE
<?php
echo date_function($_GET['num_sec']);

function date_function($num_sec)
{
    $sec = 0;
    $min = 0;
    $hour = 0;
    $day = 0;
    $month = 0;
    $year = 0;
    
    //Keep a few const to make the calculation easier to read
    //You can use Const on the follow var, but yout need to put them outside this function.
    $year_sec = 365 * 24 * 60 * 60;
    $day_sec = 24 * 60 * 60;
    $hour_sec = 60 * 60;
    $min_sec = 60;

    //Using modulus is a quick way out
    $sec = $num_sec % 60;
    //use floor to extract only the integer portion
    $year = floor($num_sec / $year_sec);
    $day = floor(($num_sec % $year_sec) / $day_sec);
    $hour = floor(($num_sec % $day_sec) / $hour_sec);
    $min = floor(($num_sec % $hour_sec) / $min_sec);
    
    //Assuming 1 month = 30 days. You can't use 31 as it differ too much.
    //30 * 12 = 360 (diff = 5)
    //31 * 12 = 372 (diff = 7)
    if ($day > 30)
    {
        $month = floor($day / 30);
        $day = $day % 30;
    }
    //Return everything
    return
        ($year > 0?
            $year . " year" . ($year > 1? "s": "") . " "
            : ""
        ) .
        ($month + $year > 0?
            $month . " month" . ($month > 1? "s": "") . " "
            : ""
        ) .
        ($day + $month + $year > 0?
            $day . " day" . ($day > 1? "s": "") . " "
            : ""
        ) .
        ($hour + $day + $month + $year > 0?
            $hour . " hour" . ($hour > 1? "s": "") . " "
            : ""
        ) .
        ($min + $hour + $day + $month + $year > 0?
            $min . " min" . ($min > 1? "s": "") . " "
            : ""
        ) .
        ($sec + $min + $hour + $day + $month + $year > 0?
            $sec . " sec" . ($sec > 1? "s": "")
            : ""
        );
}
?>


I've included the way to call it. Let me explain a little how it works.

Initially, I declare the holding var for each of the unit. Then I declare a few var to be used as constant to hold the number of seconds for each unit. $year_sec would be the number of seconds per year. It helps if you need to read the code later. it also help to improve on the performance as php doesn't need to calculate it over and over again. Try compare this
CODE
$day = floor(($num_sec % $year_sec) / $day_sec);

with this
CODE
$day = floor(($num_sec % (365 * 24 * 60 * 60)) / (24 * 60 * 60));

or this
CODE
$day = floor(($num_sec % (31536000)) / (86400));


I don't calculate months yet, as it will be too complicate. I would rather calculate days first, then if it's more than 30, then calculate months.

The next step is the calculation. As you can see, i use modulus heavily, since it's easier. That's also how we would calculate using paper and pen, which is getting the remainder when we divide.
Without using modulus, it would look like the following, which is what I used to do before i learnt the magic of modulus
CODE
$day = floor((($num_sec - ($year * $year_sec)) / $day_sec);

It's harder to read and fix if there's a bug.

Next we calculate the months from the number of days we get.

The return might looks very complicated, basically I nested 2 tertiary operator. First is to decide if the value is zero, then don't append to the string. Second is to decide if the value is more than one, which we need to append an "s" behind. For the subsequent ones, I added up those var before it, so that it will still show if it's 0 when there's something before it. Like 2 hours 0min 3 secs. 2 hours 3 secs would look too weird. If you prefer the latter way, then remove the addition, just check for individual value.

Btw, in case you haven't know of, tertiary operator works like this
([test case]? [do this if true] : [do this if false])

It's simpler to read too instead of using if/else. Further more, you can't do everything inline if you're using if/else.
Go to the top of the page
 
+Quote Post
magiccode9
post Jun 23 2008, 03:21 PM
Post #5


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 48
Joined: 7-November 05
Member No.: 9,489



Hi, faulty.lee,


Thanks your help. I am checking out.

As the list a bit longer, might require some help !

Regards,
Eric,
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Upgrade Windows Service Pack Issues(5)
  2. New Tutorials Have Issues(2)
  3. Opensuse Issues(4)
  4. Gimp: Saving As... Issues(2)
  5. Finding Yahoo Account Creation Date(1)
  6. Spam Issues...is Astahost Selling Our Addresses?(10)
  7. Date Timezones.(2)
  8. Oracle 10g Install Issues(27)
  9. Session Issues(1)
  10. Shared Hosting Account Server Update - Known Issues And Solutions(34)
  11. Asp Date And Time Tutorial(0)
  12. The Php Date(2)
  13. Calendar And The Date () Function(0)
  14. ATI Radeon 9250 Issues(7)
  15. Grand Theft Auto 4 Launch Date(5)
  1. Youtube's Flv Player - Technical And Legal Issues(1)
  2. Calc Games(4)
  3. Oh Man, Need Help Making Basic Calc In Console App(5)
  4. Issues With Accessing The Internet(11)
  5. Working Out A Date Has Passed(6)
  6. Adding Element To Page Issues(0)
  7. Did My Account Get Hacked Into?(17)
  8. Having Network Card Issues On A Dual Boot Laptop(4)
  9. Date Rapes - State Of Mind, The Drugs Used And The Potential Reasons(8)
  10. Sata Drive Issues(2)
  11. Arraylist Issues(3)
  12. Table Issues(4)
  13. Bios Issues(5)
  14. Email Issues - Maildir Invalid(5)
  15. Current Server Issues That You May Have From The April 6th Hard Drive Swap And Cpanel Upgrade(25)


 



- Lo-Fi Version Time is now: 29th August 2008 - 08:19 AM