2

I have some events. event are show in application every week depend on inserted day.like

event one > Friday
event two > sat
event three > sun

so event one show in application every Friday 2 am to 2 am

I am confused how to manage and 2 am to 2 am. I already create a logic but its cant give me right calculation

    $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;
                }

how can i show each event exactly 2 am to 2 am depend on inserted day

suppose now 12.00 AM so day is Friday but event one will be show from 2.00 AM to next 1.59AM then event two will be show from 2.00 AM to 1.59 AM (sat).

AS this way next weak automatically events will be shown

Grathand72
  • 53
  • 6
  • Are you wanting to show the events in the 24 period from 2AM on day 1 to 2AM on day 2? – Mark Hill May 01 '15 at 16:01
  • 2
    Use `$finalday = date('w', $input);` rather than that ugly switch statement – Mark Baker May 01 '15 at 16:02
  • Also for `$event_time` set it to `$event_time = date('H')` this might make it easier to manipulate your code and make the logic statement easier to come about – Mark Hill May 01 '15 at 16:04

2 Answers2

0

Try something like this just to make your code look a little bit more elegant and not so clunky

$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);

Let me know if something like this works for you. I'm sorry but I was having a hard time translating your English, I tried my best :) I hope this helps you, if not please let me know and I will help you fix it so that it does.

Here's a paste on CodePad where you can play around with the code a little bit if you want http://codepad.org/SLcTeGEt

Mark Hill
  • 1,769
  • 2
  • 18
  • 33
0

Try this program... maybe is what you want. (Your question is very confusing.)

/* 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 );
}

?>