0

希望有人能告诉我这个小调试脚本发生了什么。

<?PHP
// function generates the list of times for
function generateTimes($date) { 

 $currentDate = $date; 
 echo "RECEIVED CURRENT DATE: " . date("m/d/Y g:iA", $currentDate) . "<br /><br />"; 

 for($i = 0; $i < 48; $i++) { 
  echo date("g:iA", $currentDate) . "<br />"; 
  $currentDate = mktime(date("g", $currentDate),date("i", $currentDate)+30,0,date("m", $currentDate),date("d", $currentDate),date("Y", $currentDate)); // 30 minutes
 } 

} 
if (isset($_POST['generate_date'])) { 
 echo "Date Stamp: " . strtotime($_POST['date']) . "<br /><br />";
 echo "The time you entered: " . date("r", strtotime($_POST['date'])) . "<br /><br />"; 
 generateTimes($_POST['date']); 

} 
echo "<form method=post action='timestampgen.php'>";
echo "<input type=text name='date' />"; 
echo "<input type=submit name='generate_date' value='Generate Time Stamp' />"; 
echo "</form><br /><br />"; 

?>

我提交了一个日期,例如 10/1/10 12:00AM,我希望它生成 30 分钟的时间间隔.. 但它似乎不起作用,我认为它与我的 mktime 参数有关

我整天都在做某事,所以这可能是我要疯了。

4

4 回答 4

2

如何使用strtotime()

function generateTimes($date) {
    // convert to timestamp (make sure to validate the $date value)
    $currentDate = strtotime($date);

    for ($i=0; $i<48; $i++) {
        echo date("g:iA", $currentDate) . "<br />"; 
        $currentDate = strtotime('+30 minutes', $currentDate);
    }
}
于 2010-10-01T19:58:32.953 回答
0

获得当前时间后,您只需将 1800(30 分钟乘以 60 秒)添加到日期戳值,以将其提前半小时;没有必要一遍又一遍地使用 mktime()。

于 2010-10-01T19:57:10.670 回答
0
<?  
 $currentDate = $date;  
 echo "RECEIVED CURRENT DATE: " . date("m/d/Y g:iA", strtotime($currentDate));    

 for($i = 0; $i < 48; $i++) {  
   $currentDate= date("m/d/Y g:iA",strtotime("$currentDate +30 minutes"));  
   echo $currentDate."<br/>";  
 }  
?>  
于 2010-10-01T19:59:06.900 回答
0
start = new DateTime('10/1/10 12:00AM');
$interval = new DateInterval('PT30M');

foreach(new DatePeriod($start, $interval, 48) as $time)
{
        echo $time->format('Y-m-d g:iA')."\n";
}

产量:

2010-10-01 12:00AM
2010-10-01 12:30AM
2010-10-01 1:00AM
2010-10-01 1:30AM
...
2010-10-01 11:30PM
2010-10-02 12:00AM

您可以将DatePeriod::EXCLUDE_START_DATE第四个参数传递给DatePeriod的构造函数以跳过第一个条目。

于 2010-10-01T20:03:06.990 回答