PHP strtotime数组排序问题


php strtotime array sorting issue

我想首先感谢你花时间阅读这篇文章。我希望有人能好心地帮助我,因为我刚刚开始学习PHP。如果我没有使用正确的术语来描述我的问题,请原谅我。

我的数组排序有问题。

我的数组是这样的:

<?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>";
?>

按以下顺序输出复选框:

Mon 6/3
6/4

6/5结婚星期二星期四6/6

6/7星期五坐6/8太阳6/2

我想让今天(在这个例子中是6月2日)先显示,然后接下来的6天按顺序显示。

当我使用print_r显示原始输出时,它看起来像这样:<>之前的数组([1370239200] =比;数组((天)=比;周一[abbrev] =比;我的)[1370325600] =比;数组((天)=比;周二[abbrev] =比;星期二)[1370412000] =比;数组((天)=比;周三[abbrev] =比;结婚)[1370498400] =比;数组((天)=比;周四[abbrev] =比;星期四)[1370584800] =比;数组((天)=比;星期五[abbrev] =比;星期五)[1370671200] =比;数组((天)=比;周六[abbrev] =比;坐)[1370757600] =比;数组((天)=比;周日[abbrev] =比;太阳))之前

Sunday显示的时间戳比其他日子大,这是因为它以某种方式输出下星期天的时间戳而不是今天的时间戳吗?

最初,我每天使用两个if/else语句创建复选框和标签。由于无法以这种方式对它们进行排序,我试图创建一个数组,这就是我现在遇到的问题。

我的原始代码(这个例子是周二的)是这样的:

            <?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'))); 
        }
        ?>

是否有更好的方法来创建一个数组来实现这一点,并按正确的顺序排序?我错过了一些简单的与我已经创建的数组吗?

如果您能提供任何帮助,我将不胜感激。

谢谢。

据我所知,为了达到目的,你把事情复杂化了:

for ( $i=0; $i<6; $i++ ) {
  echo date('D n/j', strtotime("+$i day")) . '<br />';
}

日子自然会按照你想要的方式发展,所以你不妨利用这个事实—避免把所有东西都分类。你也可以使用php的日期格式来避免存储短格式的日期。

更新

这应该给你一个你可以做什么的想法,我建议你阅读一下你可以使用PHP的日期函数—这是相当有用的:)

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));
}

Sunday显示的时间戳比其他日子大,这是因为它以某种方式输出下星期天的时间戳而不是今天的时间戳吗?

是的,这就是你写的代码:strtotime('next sunday')

如果你想要今天和接下来的6天,试着用数字,例如+1天,…, +6天:http://www.php.net/manual/en/dateinterval.createfromdatestring.php

将" next sunday "更改为" sunday ",并将其作为第一个数组元素,您应该可以…