tags:

views:

57

answers:

2

i want to create a table where first column have timing in below style


timing | user1 | user2 | user3
-------------------------------
9 AM   |       |       |
10 AM  |       |       |
.      |       |       |
.      |       |       |
.      |       |       |
6 PM   |       |       |
7 PM   |       |       |

is it possible to use range() for creating timing list, if yes then please tell me , or if not then suggest me better method.

UPDATE: when i use range(strtotime('9 AM'),strtotime('7 PM'),86400) it returns bool(false) Thanks always. m i applying wrong way?

A: 

I can't see how range() can be used here.
I'd suggest to use mktime+date in the loop, to increment time by 1 hour and format it with am pm format.

Col. Shrapnel
+2  A: 

Can't do that with range alone, but you can do

date_default_timezone_set('GMT');
foreach(range(9,19) as $hour) {
    echo date('g A', $hour*3600);
}

which would give

9 AM 10 AM 11 AM 12 PM 1 PM 2 PM 3 PM 4 PM 5 PM 6 PM 7 PM

Your approach would work too, if you take one hour for step instead of one day:

range(strtotime('9 AM'), strtotime('7 PM'), 3600);

but keep in mind that it is much quicker to just have an an array with these values hardcoded somewhere instead of calculating them on the fly each time you need them.

Gordon
yes, that i want exactly! thanks.... and i need these value on the fly, i m not gonna save it anywhere, thats y i want to create an array on the fly
diEcho
can't we trail leading zero??
diEcho
y u use -1 in `$hour*3600 -1` it display 8 AM to 6 PM then.
diEcho
i"ll use hardcore, okay sir,but just for enrich my knowledge i want to do thing in easy way.
diEcho
Thanks Gordon sir, please use `echo` outside of `ltrim`
diEcho
@I Like PHP sorry, was my fault. I forgot the time would be evaluated to my timezone, which is GMT+1, so I had to do -1 to get the right time. See above, using `date` will give the exact result you want.
Gordon