douyan6871
douyan6871
2014-05-29 11:02
浏览 36

将PHP脚本转换为C#

I am currently stuck right now. I have moved to C# but one of the scripts in my PHP won't work when i convert it to C#. I have some problems with the array of minutes. Here is my PHP Script:

<?php
$start_hour = 10;
$end_hour = 22;
$minutes_array = array("15", "30", "45");
for($i=$start_hour; $i<($end_hour + 1); $i++){
    $string = $i . ':00';
    echo '<option value="' . $string . '">' . $string . '</option>';
    if($i != $end_hour){
        for($j=0; $j<sizeof($minutes_array); $j++){
            $string = $i . ':' . $minutes_array[$j];
             echo '<option value="' . $string . '">' . $string . '</option>';
        }
    }
}
?>

What it does is it outputs list items from 10 to 22 with 15, 30 and 45 between every count. So it looks like this

DropdownList
10:00
10:15
10:30
10:45
11:00
11:15
etc..

And here is my C# code so far:

int Opened = 8;
    int Closed = 22;

    for (int i = Opened; i < (Closed); i++)
    {
        string String = i + ":00";
        Response.Write(String);
        if (i != Closed)
        {
            for(int j = 0; j<sizeof(); j++)
            {
                String = i + ":" + 
            }
        }
    }

Can anyone help me converting this to C#? It would really make my day!

Thanks in advance, Jens

图片转代码服务由CSDN问答提供 功能建议

我现在被困住了。 我已经转移到C#但是当我将它转换为C#时,我的PHP中的一个脚本将无法工作。 我有几分钟的问题。 这是我的PHP脚本:

 &lt;?php 
 $ start_hour = 10; 
 $ end_hour = 22; 
 $ minutes_array = array(“15”,“  30“,”45“); 
for($ i = $ start_hour; $ i&lt;($ end_hour + 1); $ i ++){
 $ string = $ i。  ':00'; 
 echo'&lt; option value =“'。$ string。'”&gt;'  。  $ string。  '&lt; / option&gt;'; 
 if($ i!= $ end_hour){
 for($ j = 0; $ j&lt; sizeof($ minutes_array); $ j ++){
 $ string = $ i。  ':'。  $ minutes_array [$ j]; 
 echo'&lt; option value =“'。$ string。'”&gt;'  。  $ string。  '&lt; / option&gt;'; 
} 
} 
} 
?&gt; 
   
 
 

它的作用是将列表项从10输出到 22每个计数之间有15,30和45。 所以它看起来像这样

DropdownList
10:00
10:15
10:30
10:45
11:00
11:15
等等。

到目前为止,这是我的C#代码:

  int Opened = 8; 
 int Closed = 22; 
 
 for(int i = Opened; i&lt;(Closed); i ++)
 {\  n string String = i +“:00”; 
 Response.Write(String); 
 if(i!= Closed)
 {
 for(int j = 0; j&lt; sizeof(); j ++)\  n {
 String = i +“:”+ 
} 
} 
} 
   
 
 

任何人都可以帮我转换为C#吗? 这真的会成为我的一天!

提前致谢,Jens

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doudeng1870
    doudeng1870 2014-05-29 11:29
    已采纳
    var startHour = new DateTime(2000, 01, 01, 08, 00, 00);
    var endHour = new DateTime(2000, 01, 01, 22, 00, 00);
    var step = TimeSpan.FromMinutes(15);
    for (var time = startHour; time <= endHour; time += step)
    {
        Console.WriteLine(time.ToString("HH:mm"));
    }
    
    点赞 评论
  • duanla3319
    duanla3319 2014-05-29 11:18

    in php you are doing the following code for($i=$start_hour; $i<($end_hour + 1); $i++){ While in c# you are not adding the value. for (int i = Opened; i < (Closed); i++)

    点赞 评论
  • dtvx3420
    dtvx3420 2014-05-29 11:19

    using datetime variables

            var open = 8;
            var close = 22;
    
            var c = new DateTime(2014, 1, 1, open, 0, 0);
            var d = new DateTime(2014, 1, 1, close, 0, 0); ;
    
            while (c < d)
            {
                Console.WriteLine(string.Format("{0}:{1}", c.ToString("hh"), c.ToString("mm")));
                c = c.AddMinutes(15);
            }
    
    点赞 评论
  • dqab0824
    dqab0824 2014-05-29 11:28

    The below code will do what you need:

    List<string> Time = new List<string>();
    
    int hourStart = 8;
    int hourFinish = 22;
    
    for (int h = hourStart; h <= hourFinish; h++)
    {
        for (int m = 0; m < 60; m = m + 15)
        {
            string hours;
            string minutes;
    
            // this section is used to format the 0 to 00
            if (h == 0)
                hours = "00";
            else
                hours = h.ToString();
    
            if (m == 0)
                minutes = "00";
            else
                minutes = m.ToString();
    
            string time = hours + ":" + minutes;
    
            Time.Add(time);
        }
    }
    
    点赞 评论

相关推荐