How to turn maintenance during certain hours at night for any site.

Insert this code into index.php or wp-config.php and the site will go in maintenance mode between 10pm and 7am every day.

<?php

////////////////////////////////////////////////////////////////////////////////////////////////////
$current_time = strtotime('now');
$cur_hour = date('H', $current_time);
//$cur_hour = 5; // uncomment if you want to trigger a down for maintenance right now.

// We're checking for an IP address just in case because the the script can be run from the command line; if that is the case we will skip maintenance.
if (!empty($_SERVER['REMOTE_ADDR']) && ($cur_hour >= 22 || $cur_hour < 7)) {
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600'); // 1h

die('Down for maintenance.');
}
////////////////////////////////////////////////////////////////////////////////////////////////////

Notes:
Please make sure you don't forget about this code and make sure you remove it when you don't need it. Sites should be available 99.9% of the time

This approach assumes FTP/shell access to the files. Also if using WordPress similar functionality can be achieved with a plugin.
Use whichever is easier for you.

Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer