【问题标题】:PHP getting weekday hoursPHP获得工作日时间
【发布时间】:2013-03-06 04:50:29
【问题描述】:

我有一个代码,我可以在其中保存 MySQL 中特定业务的工作时间和工作日。从 MySQL 获取数据后,输出如下:

Array ( [open_hours] => 0 10 0,1 10 0,2 10 0,3 10 0,4 10 0,5 10 0,6 10 0 )

0 10 0 仅表示Mon 10am 12am,如果是这样的话,0 15 18 表示Mon 3pm 6pm。这不是问题,因为我已经通过以下代码解决了这个问题:

$openHrs = explode(",", $openHrs['open_hours']);

        $weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        $res      = array();
        foreach($openHrs as &$temp) {

            $temp = explode(" ", $temp);

            $temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
            $temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
            $res[]   = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
        }

现在我如何编写更高级的代码,以便如果今天是星期一我只需要显示星期一的输出结果?哪个是 0 10 0Mon 10am 12am ?感谢您的帮助

【问题讨论】:

  • 在上面的代码中你在哪里存储一天的数值??
  • @Sriniwas 你是什么意思?
  • 没什么……只是误会……:)

标签: php mysql datetime


【解决方案1】:

你可以用这段代码解决这个问题

     for($i=0;$i<=6;$i++) 
     {
     if($res[$i]==date('w')) //get today's week number
           {
                echo $res[$i]." ".$temp[1]." ".$temp[2];              
            } 
     }

     for($i=0;$i<=6;$i++) 
     {
     if($res[$i]==date('D')) // get today's weekday
           {
                echo $res[$i]." ".$temp[1]." ".$temp[2];              
            } 
     }

【讨论】:

    【解决方案2】:

    如果你想用今天的工作日过滤掉open_hours,下面的代码应该可以。

    $openHrs = explode(",", $openHrs['open_hours']);
    
        $weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        $res      = array();
    
        $todayWeekDay = date('D'); // get today's weekday
        $todayWeekNum = array_search($todayWeekDay, $weekdays); // get number for today's weekday
    
        foreach($openHrs as &$temp) {
    
            $temp = explode(" ", $temp);
    
            $temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
            $temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
            // only add the item where the weekday is equal to today's
            if ($temp[0] == $todayWeekNum) {
                 $res[]   = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
            }
        }
    

    【讨论】:

      【解决方案3】:
          $i; // the weekday; comes as input
      
          $openHrs = explode(",", $openHrs['open_hours']);
      
          $weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
          $res      = array();
          $temp = explode(" ", $openHrs[$i]);
      
          $temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
          $temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
          $res   = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
      

      【讨论】:

        【解决方案4】:

        这听起来是个棘手的问题,但看看是否值得尝试:

        $open_hours = array(Mon 10 0,Tue 10 0,Wed 10 0,Thu 10 0,Fri 10 0,Sat 10 0,Sun 10 0 );
        $today = date('D');
        $associate_array_key = array_keys($open_hours, $today);
        $openHrs = explode(" ", $open_hours[$associate_array_key]);
        foreach($openHrs as &$temp) {
          $temp[0]; 
          $temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
          $temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
        }
        

        这只是我能想到的概念,代码肯定需要一些操作!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-14
          • 1970-01-01
          • 1970-01-01
          • 2019-09-26
          • 1970-01-01
          • 1970-01-01
          • 2013-04-25
          • 2022-01-14
          相关资源
          最近更新 更多