开发者

in PHP how do i use range() for creating timing list?

i want to create a table where first column have timing in 开发者_如何学JAVAbelow 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?


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.

And of course you can also use DateInterval. Have a look around StackOverflow for examples.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜