Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
13 posts Page 2 of 2
Update: for consecutive increasing (no reset each hour) see this post: viewtopic.php?f=8&t=11424

A known way to create dropdown for time selection is to type in manually editable's opt_values as in following (non-optimal) example.
Code: Select all
<cms:editable name="start_time" label="Time From (24 Hrs)"
    opt_values=' Unspecified |
              00:00 | 00:30 | 01:00 | 01:30 | 02:00 | 02:30 | 03:00 | 03:30 |
              04:00 | 04:30 | 05:00 | 05:30 | 06:00 | 06:30 | 07:00 | 07:30 |
              08:00 | 08:30 | 09:00 | 09:30 | 10:00 | 10:30 | 11:00 | 11:30 |
              12:00 | 12:30 | 13:00 | 13:30 | 14:00 | 14:30 | 15:00 | 15:30 |
              16:00 | 16:30 | 17:00 | 17:30 | 18:00 | 18:30 | 19:00 | 19:30 |
              20:00 | 20:30 | 21:00 | 21:30 | 22:00 | 22:30 | 23:00 | 23:30 |'
    type='dropdown'
/>


What if we need 5 minutes step? It could become a very sad experience.. Not to mention if step is 3,2,1 minutes ;)
00:00 | 00:05 | 00:10 | 00:15 | 00:20 | 00:25 | 00:30 | 00:35 | 00:40 | 00:45 | 00:50 | 00:55 |
01:00 | 01:05 | 01:10 | 01:15 | 01:20 | 01:25 | 01:30 | 01:35 | 01:40 | 01:45 | 01:50 | 01:55 |
....
23:00 | 23:05 | 23:10 | 23:15 | 23:20 | 23:25 | 23:30 | 23:35 | 23:40 | 23:45 | 23:50 | 23:55 |


The solution is to make such printing programmatically.
Code: Select all
<cms:func 'time_options' minute_steps='30' >

    <cms:set v_step_mod     = "<cms:mod '60' minute_steps />"  />
    <cms:set v_step_count   = "<cms:div "<cms:sub '60' v_step_mod />" minute_steps />"  /><cms:ignore>
            // "v_step_count" is how many steps to perform: '17' fit 3 times within '60'
    </cms:ignore>
   
   
    <cms:if v_step_mod>
        <cms:incr v_step_count /><cms:ignore>
            // if '60' dividable without trail then ok
            // otherwise add a step
        </cms:ignore>
    </cms:if>
   
   
    <cms:repeat count='24' startcount='0'><cms:ignore>
            // repeat 24 times, as hours
        </cms:ignore>
       
        <cms:set v_current_hour = k_count />


        <cms:repeat count = v_step_count startcount='0'><cms:ignore>
                // repeat "v_step_count" times for each hour
                // '00:00:00 +5 hours 30*0 minutes'
                // '00:00:00 +5 hours 30*1 minutes'
            </cms:ignore>
            <cms:date date="00:00:00 +<cms:show v_current_hour /> hours <cms:mul k_count minute_steps /> minutes" format="H:i" /> |
        </cms:repeat>

       
        <cms:ignore>
            <br/><!-- optional html for preview -->
        </cms:ignore>   
       
    </cms:repeat>
   
</cms:func>


By calling the function above the result is printed as expected.
Code: Select all
<cms:call 'time_options' minute_steps='30' /><hr/>
<cms:call 'time_options' minute_steps='17' /><hr/>
<cms:call 'time_options' minute_steps='5' /><hr/>


It is handy to put this function ("cms:func" part) in a snippet and with such motion our editable definition becomes as follows:
Code: Select all
<cms:embed 'functions/date-time/time_options.html' />

<cms:editable name="start_time" label="Time From (24 Hrs)"
    opt_values=" Unspecified | <cms:call 'time_options' minute_steps='10' />"
    type='dropdown'
/>


Regards
I have a datetime editable whose value is saved through a DBF as:

Code: Select all
<cms:editable name="departure_time" label="Departure Time" type="datetime" show_labels="0" allow_time='1' am_pm='0' only_time='1' default_time='@current' minute_steps="1" />


Now is it possible to add 5hours and 21minutes to the value of the above region and display the new time as
Code: Select all
<cms:date new_departure_time format='H:i' />


For example:
if value saved in the departure_time is: 23:15
so after adding the 5hrs and 21mins the value should be: 04:36

I am using php code to achieve this (currently) but was eager to know if the same can be done using couch tags:
Code: Select all
<cms:php>
$date = new DateTime("<cms:date departure_time format='H:i' />");
$date->add(new DateInterval('PT10H'));
echo $date->format('H:i');
</cms:php>


Please advice.

Regards,
GenXCoders
Image
where innovation meets technology
genxcoders wrote: Please advice.

@genxcoders, do you have any issues while using my code examples from this topic? Please clarify.
13 posts Page 2 of 2

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 5 guests