In PHP, how can I use range () to create a sync list?

I want to create a table where the first column has a time like below


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

      

Can it be used range()

to create a sync list, if yes then please tell me, or if not, please suggest me a better method.

UPDATE:  when i use range(strtotime('9 AM'),strtotime('7 PM'),86400)

it returns bool (false) Thanks always. Am I applying the wrong path?

+2


a source to share


2 answers


Can't do it with range

, 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 will work too if you take one hour per step instead of one day:

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

      

but keep in mind that it is much faster to simply have an array of these values ​​encoded somewhere, rather than calculating them on the fly every time you need them.

And of course you can also use DateInterval

. Check out the StackOverflow example.

+2


a source


I don't see how range () can be used here.
I would suggest using mktime + date in a loop to increment the time by 1 hour and format it as am pm.



0


a source







All Articles