PHP查找从开始时间到下一个重复事件的天数

时间:2013-03-14 23:00:34

标签: php calendar intervals

我有一个日历,其中包含用户从安装开始日期输入的事件。 这些事件总是从开始日期开始,每隔X天,几个月或几年重复,我们不知道结束日期。 我需要找到以unix时间戳格式发生的下一个事件。 我试图找出内置的PHP DateInterval类,但我无法弄清楚如何没有已知的结束日期。

这是我到目前为止所拥有的

/* User Input */
$installdate="315558000";// 1st Jan 1980 
$Every = 1; // Could be 1 =>  
$DateOption = 'Months'; // Could be 'Days' 'Months' or 'Years'

#Possible user selections 
$DateJustify = array('Days'=>j,'Months'=>n,'Years'=>Y);

#Getting the next service date into printed variables for mktime
foreach($DateJustify as $keyDate => $dt ){
${$keyDate}=date($dt,$installdate);
if($DateOption == $keyDate )
${$keyDate} += $Every; 
}

这将返回mktime时间戳的所有内容,但它只是从installdate中提供了下一个日期。最终,秒,分钟或小时无关紧要,因为安装时间从一天开始

1 个答案:

答案 0 :(得分:0)

使用DateTime类将字符串转换为时间。第二个参数接受时区,使用DateTimeZone

$z = new DateTimeZone("Europe/Berlin");
$d = new DateTime("next wed +3 day", $z);
echo $d->format("d.m.Y H:i");
echo $d->format("U"); # or echo $d->getTimestamp(); on PHP > 5.3.0

DateTime::format()方法接受date()函数等格式。