We help IT Professionals succeed at work.

If date and time equals then send email in php

Razzmataz73
Razzmataz73 asked
on
Good morning,
I have a php form that successfully send email.

I now need to code it so from Friday 5pm EST to Sunday 9am EST it sends to one email address.  And all other days/times it sends to another.

I have been able to get the current date and time on the server by $brokerday = date('l');
$brokerhour = date('h:i:s A');

But the next part has me stumped.
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
Most Valuable Expert 2011
Top Expert 2016

Commented:
The key things you will need to know are the timezone settings, and the days of the week.  I will try to give you a script in a moment.

Commented:
Try this.

<?php
$from_day = 5; //5 is Friday (0=Sunday, 6=Saturday)
$from_hour = '1700'; // 24hour format
$to_day = 0; //0 is Sunday
$to_hour = '0900'; // 24hour format

if (intime($from_day, $from_hour, $to_day, $to_hour)) {
	echo "Send mail to A";
} else {
	echo "Send mail to B";
}

function intime($day1, $time1, $day2, $time2) {
	// returns 1 if NOW is in the given period
	// otherwise returns 0

	$now_day = date('w');
	$now_time = date('Hi');

	if ($day2 < $day1) {
		$day2 += 7;
	}

	if (($day1 < $now_day) && ($now_day < $day2)) {
		return 1;
	} elseif ($day1 == $now_day) {
		return ($time1 <= $now_time);
	} elseif ($day2 == $now_day) {
		return ($time2 >= $now_time);
	} else {
		return 0;
	}
}

?>

Open in new window

Commented:
I replace using day index to day name and 24hour format to 12hour format. This makes this work to be easier.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Test is here: http://www.laprbass.com/RAY_temp_razz.php

outputs:
BECAUSE IT IS Sun 0903 (weekdays) I WILL SEND THE EMAIL TO regular@Gmail.com
<?php // RAY_temp_razz.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');


// PROBLEM DEFINITION: from Friday 5pm EST to Sunday 9am EST it sends to one email address.


// EMAIL ADDRESSES
$emails = array
( 'weekdays' => 'regular@Gmail.com'
, 'weekends' => 'special@Gmail.com'
)
;

// DAYS IN QUESTION
$days = array
( 'Fri' => '1700,2400'
, 'Sat' => '0000,2400'
, 'Sun' => '0000,0900'
)
;


// DETERMINE THE CURRENT DAY AND TIME
$today = date('D');
$timex = date('hi');

// MAKE AN ASSUMPTION
$key = 'weekdays';

// TEST THE CURRENT DAY, AND MAYBE TEST THE TIME
if (array_key_exists($today, $days))
{
    $times = explode(',', $days[$today]);

    // SHOULD WE CHANGE THE ASSUMPTION?
    if ( ($timex >= $times[0]) && ($timex <= $times[1]) )
    {
        $key = 'weekends';
    }
}

echo "BECAUSE IT IS $today $timex ($key) I WILL SEND THE EMAIL TO {$emails["$key"]}";

Open in new window