dourang20110122 2013-06-02 20:13
浏览 34
已采纳

php strtotime数组排序问题

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.

  • 写回答

3条回答 默认 最新

  • donglu9445 2013-06-02 20:27
    关注

    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.

    update

    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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器