我有一些事件。每周应用程序中显示的事件取决于插入的日期。像
event one > Friday
event two > sat
event three > sun
所以第一个事件每周五凌晨2点到凌晨2点在申请中显示
我很困惑如何管理,凌晨2点到凌晨2点。我已经创建了一个逻辑,但它不能给我正确的计算
$input = time();
$day = date('D', $input );
switch ($day) {
case 'Sun':
$finalday='0';
break;
case 'Mon':
$finalday='1';
break;
case 'Tue':
$finalday='2';
break;
case 'Wed':
$finalday='3';
break;
case 'Thu':
$finalday='4';
break;
case 'Fri':
$finalday='5';
break;
case 'Sat':
$finalday='6';
break;
}
$now = time();
$event_time = strtotime("02:00 am");
if( ($now - $event_time) < 0) // 5 minutes * 60 seconds, replace with 300 if you'd like
{
//before day
if($finalday=='0')
{
$query_day='6';
}
else
{
$query_day=$finalday-1;
}
}
else
{
//current day
$query_day=$finalday;
}
我如何准确地显示每个事件凌晨2点到凌晨2点取决于插入的日期
现在假设现在是上午12点所以星期五是星期五,但是事件1将从凌晨2点到下午1.59点显示,然后事件2将从凌晨2点到凌晨1.5点(坐着)显示。
通过这种方式,将显示下一个弱自动事件
答案 0 :(得分:0)
尝试这样的方法只是为了让你的代码看起来更优雅而不那么笨重
$dayOfWeek = date('w'); //0 for Sunday through 6 for Saturday
$hourOfDay = date('H'); //0-23
$eventOne = null;
$eventTwo = null;
$eventThree = null;
//logic structure to set events
if($hourOfDay >= 0 && $hourOfDay < 2){
$dayOfWeek -= 1; //set to previous day if earlier than 2AM
$dayOfWeek = $dayOfWeek == 0 ? 6 : $dayOfWeek; //quick check to set to Sunday if day was on Monday
$eventOne = $dayOfWeek;
$eventTwo = $dayOfWeek+1;
$eventThree = $dayOfWeek+2;
//single line if statements to correct weekly overflow
if($eventTwo == 7) $eventTwo = 0;
if($eventThree == 7) $eventThree = 0;
if($eventThree == 8) $eventThree = 1;
}else{
$eventOne = $dayOfWeek;
$eventTwo = $dayOfWeek+1;
$eventThree = $dayOfWeek+2;
//single line if statements to correct weekly overflow
if($eventTwo == 7) $eventTwo = 0;
if($eventThree == 7) $eventThree = 0;
if($eventThree == 8) $eventThree = 1;
}
function getDayOfEvent($event){
switch($event){
case 0: return "Sunday"; break;
case 1: return "Monday"; break;
case 2: return "Tuesday"; break;
case 3: return "Wednesay"; break;
case 4: return "Thursday"; break;
case 5: return "Friday"; break;
case 6: return "Saturday"; break;
}
}
print "Event One: ". getDayOfEvent($eventOne)."\nEvent Two: ".getDayOfEvent($eventTwo)."\nEvent Three: ".getDayOfEvent($eventThree);
如果这样的话适合你,请告诉我。对不起,但我很难翻译你的英语,我尽我所能:)我希望这对你有所帮助,如果没有,请告诉我,我会帮助你解决这个问题。
这是CodePad上的一个粘贴,如果你想要http://codepad.org/SLcTeGEt
,你可以稍微使用代码答案 1 :(得分:0)
试试这个程序......也许就是你想要的。 (你的问题很混乱。)
/* Day Of Week 0 = Sun ... 6 = Sat
* ---------------------------------
* Day Hour Result Case
* ---------------------------------
* 5 00 - 02 No event C
* 5 02 - 24 Event 1 B
* 6 00 - 02 Event 1 A
* 6 02 - 24 Event 2 B
* 0 00 - 02 Event 2 A
* 0 02 - 24 Event 3 B
* 1 00 - 02 Event 3 A
* 1 02 - 24 No Event C
* Other Other No Event C
* ---------------------------------
*/
function getEvent( $timestamp, $eventTime ) {
$d = (int) date( 'w', $timestamp ); // Day
$h = (int) date( 'G', $timestamp ); // Hour
$event = $h < $eventTime && ( $d > 5 || $d < 2 ) // Case A
? ( $d + 2 ) % 7 // Case A Result
: ( $h >= $eventTime && ( $d > 4 || $d == 0 ) // Case B
? ( $d + 3 ) % 7 // Case B Result
: null ); // Case C Result
printf ( "\n%s %02d:00 :: %s", // ... and show
date( 'D', strtotime( "Sunday +{$d} days" ) ),
$h, $event ? "Event $event" : 'No event' );
}
$eventTime = 2;
echo '<pre>';
/* Testing the getEvent function */
for ( $timestamp = mktime( 23, 0, 0, 4, 30, 2015 ); // Thu at 23:00
$timestamp <= mktime( 22, 0, 0, 5, 7, 2015 ); // Thu at 22:00
$timestamp += 3600 * 2 ) { // Each 2 hours
getEvent( $timestamp, $eventTime );
}
?>