www.webdeveloper.com
Results 1 to 10 of 10

Thread: Time imcrement

  1. #1
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062

    Time imcrement

    Hi,

    I have a start time and an end time, both in 24 hour time. I also have an incremental time, in minutes.

    So if i want to start at the start time, and populate a drop down with times updating at the specified increment, Then stop at the end time.

    Can any one provide me on any ideas or function names of how to do this, I have tried a couple of ways and researched a lot via php.net but can't seam to get it.

  2. #2
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    I have this

    PHP Code:
    <select name="time">
        <?php
        $start     
    explode(":""10:00");
        
    $end       explode(":""03:00");
        
    $increment "15";
        
    $start     mktime("0""0"$start['0'], $start['1']);
        
    $end       mktime("0""0"$end['0'], $end['1']);
        for(
    $time $start$time <= $end;){
            echo(
    "<option value=\"{$time}\">{$time}</option>");
            
    $time = ($time $increment);
        }
        
    ?>
    </select>
    Which just returns blank rows.

  3. #3
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    I did it like this because I was bored (PHP 5):

    PHP Code:
    <?php

    class TimeIterator implements Iterator
    {
        private 
    $start;
        private 
    $end;
        private 
    $interval;
        private 
    $position;
        private 
    $current;
        
        public function 
    __construct($start$end$interval)
        {
            
    $start str_pad(preg_replace('/[^\d]/','',$start), 4"0"STR_PAD_LEFT);
            
    $end str_pad(preg_replace('/[^\d]/','',$end), 4"0"STR_PAD_LEFT);
            
    $this->interval = ((int)$interval)*60;
            
    $this->start mktime(substr($start, -42),substr($start, -22));
            
    $this->end mktime(substr($end, -42),substr($end, -22));
            if(
    $this->start $this->end)
            {
                
    $this->end += 86400;
            }
        }
     
        public function 
    rewind() {
            
    $this->position 0;
            
    $this->current $this->start;
        }
     
        public function 
    current() {
            return 
    date('H:i',$this->current);
        }
     
        public function 
    key() {
            return 
    $this->position;
        }
     
        public function 
    next() {
            
    $this->position++;
            
    $this->current += $this->interval;
            return 
    true;
        }
     
        public function 
    valid() {
            return 
    $this->current <= $this->end;
        }
    }

    $dropdown '';

    $start '10:00';
    $end '03:00';
    $interval 15;
     
    foreach(new 
    TimeIterator($start$end$interval) as $k => $v)
    {
        
    $dropdown .= "<option value='{$v}'>{$v}</option>\n";
    }

    // test it
    echo "<select name='time'>\n{$dropdown}</select>";
     
    ?>
    Last edited by bokeh; 09-24-2007 at 06:58 PM.

  4. #4
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    Thanks Bokeh, Perfect, As always.

  5. #5
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    By the way your code should have looked like this:
    PHP Code:
    <select name="time">
    <?php
    $start     
    explode(":""10:00");
    $end       explode(":""03:00");
    $increment "15";
    $start     mktime($start['0'], $start['1']);
    $end       mktime($end['0'], $end['1']);

    if(
    $start $end)
    {
        
    $end += 86400;
    }

    for(
    $time $start$time <= $end$time += ($increment*60))
    {
        echo
    '<option value="'.date('H:i'$time).'">'.date('H:i'$time)."</option>\n";
    }
    ?>
    </select>
    Mine's better though from the validation point of view at the very least.
    Last edited by bokeh; 09-24-2007 at 07:26 PM.

  6. #6
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    Yes the way i was doing it will not allow the time to span to the next day, where your will, which is perfect.

  7. #7
    Join Date
    Feb 2007
    Location
    Lower Paleolithic
    Posts
    32
    Quote Originally Posted by bokeh
    I did it like this because I was bored
    Hi bokeh,

    Is this a case of someone, at last, admitting why they write OOP code?

    I have been reading a bit about OOP lately which is why I asked about database classes in the other thread but here I just can't see the point. Your class seems a lot of trouble compared with the procedural example. What is the point? Or do people just write OOP because they are bored?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,145
    When comparing one specific, isolated, tiny application done in OOP style to one done in procedural style, it will be difficult to find any advantage (assuming both are done well). As applications get larger, OOP starts to overtake procedural by allowing the use of OOD[esign] concepts and the creation of highly modular and loosely coupled code. Together this makes it easier to get it closer to being right the first time, and then also easier to modify as you test and debug it, getting you to the end of the project faster. This also makes it easier to maintain the code as bugs are discovered and new features are requested. It also makes it much, much easier to delegate tasks on a mutli-programmer project, as each programmer can work on his particular set of classes, and all he needs to know about the classes others are working on is their public interface definition.

    Lastly, and perhaps the main reason OOD/OOP was "invented", is that it greatly enhances the re-usability of code, as each class provides a clearly defined set of functionalities with an equally clearly defined interface, making it much easier to plug in to other application; as opposed to searching through several thousand lines of procedural code to find the functions you want to copy, then copying-and-pasting them into another procedural program, finding out that they depend on another function you didn't realize was called by the code, then finding out that that function uses a global variable which you don't currently have in the application, etc., etc., and so forth.

    Really lastly , I find that writing my code in OOP style makes me think more carefully about what I'm doing and forces me to break things down into "bite-sized" chunks that I can concentrate on one at a time, instead of trying to keep track of where I am in the entire program flow and jumping around in an unorganized manner.

    But OOP is by no means a programming "silver bullet", and there are more than a few very excellent programmers out there that don't like it and prefer not to use it; and using it by no means guarantees inferior programmers will suddenly become superior ones. All that being said, if you have thoughts of ever working on large projects with many programmers, you'd best learn the theory and practice of OOD/OOP, as it's here to stay for at least a while.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    I cant remember the last time I worked on a large project without using OOP.

    Being able to extend existing classes is my favourite thing about OOP.
    Quote Originally Posted by temp.user123
    You know... You're not so smart. Do you need me to educate you?
    If you say, "please," (and do so, nicely) then I will show you where you're dead wrong.

  10. #10
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    PHP Code:
    foreach(new TimeIterator($start$end$interval) as $k => $v
    I just did it like that because I fancied writing a lazy iterator. Notice above how the class call can be used directly in a foreach loop even though it doesn't return an array.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles