PHP日期/时间和mktime

时间:2010-10-01 19:47:04

标签: php date time mktime

希望有人可以告诉我这个小调试脚本是怎么回事。

<?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:00 AM,我想让它产生30分钟的时间间隔..但它似乎没有工作,我认为它与我的mktime参数有关

我整天都在做一些事情,所以这可能是我疯了。

4 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分: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/>";  
 }  
?>  

答案 3 :(得分: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的构造函数,以跳过第一个条目。