current time between two time values

Discussion in 'PHP' started by jacka, Jun 30, 2009.

  1. #1
    Hi

    I wish to display a message letting visitor know that server is busy backing up data between 18.10 and 21.10 (6:10 - 9:10) every working day, using php.

    I have written this code but I can not figure out how to incorporate minutes as well as hours. ( At present this only checks the hour).

    I am sure there are better ways of writing even this code.

    Can some one please help me.

    
    
    $tim = localtime(time(),true);
    
    if( (date ("l") =="Monday" || date ("l") =="Tuesday" || date ("l") =="Wednesday" || date ("l") =="Thursday" || date ("l") =="Friday" ) && 
     
    !($tim['tm_hour']>=18 && $tim['tm_hour']<21 ))
     {
     echo "correct stock display";
     }
      else
      {
       echo "NOT correct stock display";
     }
     
    
    
    Code (markup):

     
    jacka, Jun 30, 2009 IP
  2. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi
    can some one plz tell me if I am on the right track:

    I am not at all an expert on php.

    I have used todays current hour and minutes and converts them to 24 hour number.
    then I have simply compared it with numeric 18.166 (18:10 time) and 21.166 (21:10) and if it is between them and on weeks days I will display a message saying backup not allowed.
    Thank for looking at it.
     
    jacka, Jun 30, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I think you're getting overly complicated on this. Just to clarify, you want to display one message if it is a weekday, between 18:10 and 21:10? If not, please correct me.

    Try something like this:

    
    $message = 'NOT correct stock display';
    
    switch(date('N')):
    	
    	case 1: //Monday
    	case 2: //Tuesday
    	case 3: //Wed
    	case 4: //Thurs
    	case 5: //Fri
    	
    	$start = mktime(18,10);
    	$end = mktime(21,10);
    	$now = time(); //Current timestamp
    	
    	if($now > $start && $now < $end):
    	
    		$message = 'correct stock display';
    		
    	endif;
    	
    	break;
    endswitch;
    
    echo $message;
    
    PHP:
    The messages may be backwards, but this should do what you need.
     
    jestep, Jun 30, 2009 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Job done, nice and sweet.
    I suppose thats the difference between novice and expert.

    As always when the problem is solves , it looks simple.

    Many thanks.
     
    jacka, Jul 1, 2009 IP
  5. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi
    As i am a beginner, can you please explain how does it know what day of week it is?

    I can understand the time check but cant understand how I can call the switch(date('N')): part.
    Thanks
     
    jacka, Jul 1, 2009 IP
  6. AlilG

    AlilG Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it know the day of week from this function:
    
    date('N')
    
    PHP:
    this is the current day of week (in number).
    it return (today) weekday number!

    for example
    if you run this function (date('N')) at monday you will got 1
    if you run it at Tuesday you will got 2 and same.....
     
    AlilG, Jul 1, 2009 IP
  7. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Awesome.

    You are the best.
    Thanks
     
    jacka, Jul 1, 2009 IP
  8. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi
    just a slight complication.

    My PHP version is 4.4.2 and therefore can not use date('N') function.

    is there any easy workaround please?

    thanks
     
    jacka, Jul 1, 2009 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    Change to this:

    
    
    switch(date('w')):
    
    
    PHP:
    This will still work fine for M-F, But Saturday and Sunday are different, if you ever need to use them. Saturday is 6 and Sunday is 0.

    With 'N', Saturday is 6 and Sunday is 7.
     
    jestep, Jul 1, 2009 IP