Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    create time range by minutes, secs, or hours

    I posted in response to a thread but it is fairly generic and may be of use to more than a few people.
    PHP Code:
    <?php

    /**
     * create_time_range 
     * 
     * @param mixed $start start time, e.g., 9:30am or 9:30
     * @param mixed $end   end time, e.g., 5:30pm or 17:30
     * @param string $by   1 hour, 1 mins, 1 secs, etc.
     * @access public
     * @return void
     */
    function create_time_range($start$end$by='30 mins') {

        
    $start_time strtotime($start);
        
    $end_time   strtotime($end);

        
    $current    time();
        
    $add_time   strtotime('+'.$by$current);
        
    $diff       $add_time-$current;

        
    $times = array();
        while (
    $start_time $end_time) {
            
    $times[] = $start_time;
            
    $start_time += $diff;
        }
        
    $times[] = $start_time;
        return 
    $times;
    }

    // create array of time ranges
    $times create_time_range('9:30''17:30''30 mins');

    // more examples
    // $times = create_time_range('9:30am', '5:30pm', '30 mins');
    // $times = create_time_range('9:30am', '5:30pm', '1 mins');
    // $times = create_time_range('9:30am', '5:30pm', '30 secs');
    // and so on

    // format the unix timestamps
    foreach ($times as $key => $time) {
        
    $times[$key] = date('g:i:s'$time);
    }


    print 
    '<pre>'print_r($timestrue).'</pre>';
    /*
     * result
     *
    Array
    (
        [0] => 9:30:00
        [1] => 10:00:00
        [2] => 10:30:00
        [3] => 11:00:00
        [4] => 11:30:00
        [5] => 12:00:00
        [6] => 12:30:00
        [7] => 1:00:00
        [8] => 1:30:00
        [9] => 2:00:00
        [10] => 2:30:00
        [11] => 3:00:00
        [12] => 3:30:00
        [13] => 4:00:00
        [14] => 4:30:00
        [15] => 5:00:00
        [16] => 5:30:00
    )
     
    */

    ?>
    Last edited by fci; 05-26-2006 at 08:44 PM.

  • #2
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hey there,

    I found this function from ya a while ago - but I recently needed it to return a 24 hour timestamp, rather going from 12:00:00 to 1:00:00 - my script treated this as 1am, but it gave me some inspriation, and I wrote this quickly:

    Code:
    function create_time_range2($start, $end, $by) {
    	// Create UNIX Timestamps
    	$start_exp = explode(":",$start);
    	$end_exp = explode(":",$end);
    	$start_time = mktime($start_exp[0],$start_exp[1],$start_exp[2],date(m),date(d),date(Y));
    	$end_time = mktime($end_exp[0],$end_exp[1],$end_exp[2],date(m),date(d),date(Y));
    		
    	setlocale(LC_TIME,"english-aus");
    		
    	// Create the array
    	$times = array();
    	for ( ;$start_time < $end_time; ) {
    		$times[] = date('H:i:s',$start_time);
    		$temp = ($start_time + (60 * $by));
    		$start_time = $temp;
    	}
    	$times[] = date('H:i:s',$start_time);
    	return $times;
    }
    Hope it helps
    Jesse.

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to show this type of time range in a drop down menu?

    I came across the below code, but I don't know how to display the results in a drop down menu, where user selects one of the option. Can somebody please help as I am quite new with PHP?
    At the end 'print' is used to show the results, but how would I write the code as a part of select tag? something like:

    <option value=
    "<?php
    echo "$times[$key]";
    ?>"
    >

    </option>

    Does somebody know the answer to this as I really don't know?




    Quote Originally Posted by fci View Post
    I posted in response to a thread but it is fairly generic and may be of use to more than a few people.
    PHP Code:
    <?php

    /**
     * create_time_range 
     * 
     * @param mixed $start start time, e.g., 9:30am or 9:30
     * @param mixed $end   end time, e.g., 5:30pm or 17:30
     * @param string $by   1 hour, 1 mins, 1 secs, etc.
     * @access public
     * @return void
     */
    function create_time_range($start$end$by='30 mins') {

        
    $start_time strtotime($start);
        
    $end_time   strtotime($end);

        
    $current    time();
        
    $add_time   strtotime('+'.$by$current);
        
    $diff       $add_time-$current;

        
    $times = array();
        while (
    $start_time $end_time) {
            
    $times[] = $start_time;
            
    $start_time += $diff;
        }
        
    $times[] = $start_time;
        return 
    $times;
    }

    // create array of time ranges
    $times create_time_range('9:30''17:30''30 mins');

    // more examples
    // $times = create_time_range('9:30am', '5:30pm', '30 mins');
    // $times = create_time_range('9:30am', '5:30pm', '1 mins');
    // $times = create_time_range('9:30am', '5:30pm', '30 secs');
    // and so on

    // format the unix timestamps
    foreach ($times as $key => $time) {
        
    $times[$key] = date('g:i:s'$time);
    }


    print 
    '<pre>'print_r($timestrue).'</pre>';
    /*
     * result
     *
    Array
    (
        [0] => 9:30:00
        [1] => 10:00:00
        [2] => 10:30:00
        [3] => 11:00:00
        [4] => 11:30:00
        [5] => 12:00:00
        [6] => 12:30:00
        [7] => 1:00:00
        [8] => 1:30:00
        [9] => 2:00:00
        [10] => 2:30:00
        [11] => 3:00:00
        [12] => 3:30:00
        [13] => 4:00:00
        [14] => 4:30:00
        [15] => 5:00:00
        [16] => 5:30:00
    )
     
    */

    ?>

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    PHP Code:
    <select name="time">
    <?php
    $times 
    create_time_range('9:30''17:30''30 mins');
    foreach (
    $times as $key => $time)
        echo 
    '<option value="' date('g:i:s'$time) . '">' date('g:i:s'$time) . '</option>';
    ?>
    </select>


  •  

    Posting Permissions

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