我有2个php函数在functions.php,一个监听表单输入并将此信息发送到数据库,这一个完美的工作,第二个运行每小时,检查是否有相同日期的输入,并检查时间是否在未来两个小时,如果是,它发送电子邮件到数据库的电子邮件
'book_time'是一个varchar(8),格式为HH:MM:SS。'book_date,是一个DATE类型,'email'是一个varchar(254),'name'是一个varchar(256)。
这是我代码,
add_action( 'elementor_pro/forms/new_record', function( $record, $ajax_handler ) {
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
global $wpdb;
$output['success'] = $wpdb->insert('bookings', array( 'name' => $fields['name'], 'email' => $fields['email'], 'field_2' => $fields['field_2'], 'message' => $fields['message'], 'book_date' => $fields['book_date'], 'book_time' => $fields['book_time'] . ":00", 'book_people' => $fields['book_people'], 'book_location' => $fields['book_location'], 'book_hrs' => explode(":", $fields['book_time'])[0], 'book_min' => explode(":", $fields['book_time'])[1]));
$ajax_handler->add_response_data( true, $output );
}, 10, 2);
function send_reminder_email() {
// Get the current time
$current_time = time();
// Connect to the database and retrieve the reservations
global $wpdb;
$reservations = $wpdb->get_results("SELECT name, email, book_date, book_time, book_hrs, book_min FROM your_table_name WHERE book_date = CURDATE() AND STR_TO_DATE(book_time, '%H:%i:%s') >= DATE_SUB(CURTIME(), INTERVAL 2 HOUR)");
// Loop through each reservation and send an email
foreach ($reservations as $reservation) {
$to = $reservation->Email;
$subject = "Reservation Reminder";
$message = "This is a reminder for your reservation on " . $reservation->Date . " at " . $reservation->Time;
mail($to, $subject, $message);
}
}
// Schedule the function to run every hour
if (!wp_next_scheduled('send_reminder_email')) {
wp_schedule_event(time(), 'hourly', 'send_reminder_email');
}
// Hook the function to the scheduled event
add_action('send_reminder_email', 'send_reminder_email');
我已经在互联网上搜索,仍然找不到任何东西,如果我尝试调用mail()与硬编码的值或$forms['email']
为第一个参数,它的工作刚刚好
我修复了发送电子邮件的代码中的missinputs(“Email”更改为“email”,“Time”更改为“book_time”,“Date”更改为“book_date”)
1条答案
按热度按时间0x6upsns1#
回显SQL结果(或手动运行)以检查它是否确实给出了结果。
您很可能会发现这是一个时区问题:您输入的测试日期/时间是您的本地时间,但数据库运行在不同的时区。
最简单的处理方法是在UTC中进行所有处理:
这适用于夏令时、跨境等。
您也可以为自己简化此过程: