I'd like to start off by saying thank you for taking the time to read this post. I hope that someone will be kind enough to help me as I am just starting to learn PHP. Please forgive me if I do not use the correct terminology to describe my issue.
I'm having an issue sorting my array.
My array looks like this:
<?php
$rooms = array(
strtotime('next monday')=>array('day'=>'monday', 'abbrev'=>'Mon'),
strtotime('next tuesday')=>array('day'=>'tuesday', 'abbrev'=>'Tue'),
strtotime('next wednesday')=>array('day'=>'wednesday', 'abbrev'=>'Wed'),
strtotime('next thursday')=>array('day'=>'thursday', 'abbrev'=>'Thu'),
strtotime('next friday')=>array('day'=>'friday', 'abbrev'=>'Fri'),
strtotime('next saturday')=>array('day'=>'saturday', 'abbrev'=>'Sat'),
strtotime('next sunday')=>array('day'=>'sunday', 'abbrev'=>'Sun'));
ksort($rooms);
foreach($rooms as $room_timestamp=>$room_info) {
if (time() > strtotime($room_info['day'])) {
print ($form->checkBox($model,'space_'.$room_info['day'], array('value' => strtotime($room_info['day']))) . $form->labelEx($model,$room_info['abbrev'].' ' . date('n/j', strtotime($room_info['day']))) . '<br />');
} else {
print ($form->checkBox($model,'space_'.$room_info['day'], array('value' => strtotime('next '.$room_info['day']))) . $form->labelEx($model,$room_info['abbrev'].' ' . date('n/j', strtotime('next '.$room_info['day']))) . '<br />');
}
}
echo "<pre>".print_r($rooms,1)."</pre>";
?>
And it is outputting the checkboxes in this order:
Mon 6/3
Tue 6/4
Wed 6/5
Thu 6/6
Fri 6/7
Sat 6/8
Sun 6/2
I'm trying to get today (in this case Sun 6/2) to show first, and then the next 6 days to show in order.
When I use print_r to display the raw output, it looks like this:
Array ( [1370239200] => Array ( [day] => monday [abbrev] => Mon ) [1370325600] => Array ( [day] => tuesday [abbrev] => Tue ) [1370412000] => Array ( [day] => wednesday [abbrev] => Wed ) [1370498400] => Array ( [day] => thursday [abbrev] => Thu ) [1370584800] => Array ( [day] => friday [abbrev] => Fri ) [1370671200] => Array ( [day] => saturday [abbrev] => Sat ) [1370757600] => Array ( [day] => sunday [abbrev] => Sun ) )
Sunday is showing a larger time stamp than the other days, is this because it is somehow outputting next Sunday's time stamp instead of today's time stamp?
Initially I was creating the checkboxes and labels using two if/else statements for each day. Unable to sort them this way I attempted to create an array which is what I am now having trouble with.
My original code (this example is for Tuesday) looked like this:
<?php
if (time() > strtotime('tuesday'))
{
echo $form->checkBox($model,'space_tuesday', array('value' => strtotime('tuesday')));
}
else
{
echo $form->checkBox($model,'space_tuesday', array('value' => strtotime('next tuesday')));
}
?>
<?php
if (time() > strtotime('tuesday'))
{
echo $form->labelEx($model,'Tues ' . date('n/j', strtotime('tuesday')));
}
else
{
echo $form->labelEx($model,'Tues ' . date('n/j', strtotime('next tuesday')));
}
?>
Is there a better way to create an array to achieve this and sort in the correct order? Am I missing something simple with the array I've already created?
Any help that you could provide would be greatly appreciated.
Thank you.
As far as I understand it, for what you want to achieve, you're overcomplicating things:
for ( $i=0; $i<6; $i++ ) {
echo date('D n/j', strtotime("+$i day")) . '<br />';
}
Days naturally progress in the way you want, so you might as well use that fact — and avoid sorting all together. You can also make use of php's date formatting to avoid having to store shortforms of days too.
This should give you an idea of what you can do, I'd suggest you read up on what you can generate using PHP's date function — it's rather useful :)
http://uk1.php.net/manual/en/function.date.php
for ( $i=0; $i<6; $i++ ) {
/// pull out our datetime into a variable
$datetime = strtotime("+$i day");
/// wrap it with an array ready for checkBox()
$value = array('value' => $datetime);
/// generate the different parts we may need from date()
list($day, $formatted) = explode(',', date('l,D n/j', $datetime));
/// place those parts where we need them
echo $form->checkBox($model, "space_" . strtolower($day), $value);
echo $form->labelEx($model, $formatted));
}
Change 'next sunday' to 'sunday', put it in as the first array element and you should be good..
Sunday is showing a larger time stamp than the other days, is this because it is somehow outputting next Sunday's time stamp instead of today's time stamp?
Yes, this is what you coded: strtotime('next sunday')
Try using numbers if you want today and next 6 days, e.g.+1 day, ..., +6 days: http://www.php.net/manual/en/dateinterval.createfromdatestring.php