I am using the code below for sending web mails and SMS notifications when users choose “A” value from the "servicerequired" drop-down menu. It works perfectly fine but I'd to change the time schedule from “Disabled” everyday from 8PM to 5AM to :

Mon to Fri “Disabled" between 8PM to 5AM and between 7AM to 4PM
Sun and Saturday “Disabled” between 8PM to 5AM

Here is my current code which stops web SMS from sending everyday from 8PM to 5AM:

if ($_POST['submit']) {
           $sms="";
           $subject="";
           $from = "";
           $service = $_REQUEST['servicerequired'] ;
           $headers = "";
           $message="message";
if($_POST['servicerequired']=='A' && (date("G")>5 && date("G")<20)  ){ 
           @mail($sms, $subject, $message, $headers);
}

The HTML Markup:

<form action="" method="post">
<select name="servicerequired">
    <option value="A">A</option>
    <option value="B">B</option>
</select>
<input value="submit" name="submit" />
</form>

This has doing my head in for a couple of days now. I have done some research and came up with this solution but I am not sure if it is the best practice or even works.

My Solution:

if($_POST['servicerequired']=='A')
{
    if(date("W")=0 || date("W")=6) && (date("G")>5 && date("G")<20)
    {
        @mail($sms, $subject, $message, $header); 
    }
    else if
    {
        (date("W")>0 && date("W")<6) && (date("G")>5 && date("G")<20)
    }
    else
    {
        @mail($sms, $subject, $message, $header);
    }
}

Appreciate your help and Please consider adding a comment if you think this post can be improved :)

有帮助吗?

解决方案

UPDATED******

Try :

if($_POST['servicerequired']=='A')
{
switch (date("N"))
{
 case 6:  //Saturday
 if(date("G")>5 && date("G")<20)
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 7:  //Sunday
 if(date("G")>5 && date("G")<20) 
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 1:  //Monday
 if(date("G")>5 && date("G")<20 && date("G")>16 && date("G")<7)
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 2:  //Tuesday
 if(date("G")>5 && date("G")<20 && date("G")>16 && date("G")<7)
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 3:  //Wednesday 
 if(date("G")>5 && date("G")<20 && date("G")>16 && date("G")<7)
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 4:  //Thursday
 if(date("G")>5 && date("G")<20 && date("G")>16 && date("G")<7)
 {
    @mail($sms, $subject, $message, $header); 
 }
 break;
 case 5:  //Friday
 if(date("G")>5 && date("G")<20 && date("G")>16 && date("G")<7)
 {
    @mail($sms, $subject, $message, $header); 
 }
}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top