Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Timely Greeting (2.0.1) A PHP script that you can use on your site to automatically display a greeting based on what time of day it is for the user. TO USE: See Readme.txt file
/*
* README.TXT -- TIMELY GREETING SCRIPT:
* By Nicolas Saad (www.nicolassaad.com)
* An PHP add-on script that you can use to automatically display a greeting based on what time of day it is for the user.
*
* INSTRUCTIONS:
* 1. To use put both greeting files in a folder named 'includes' inside your root directory. If you place them in a folder with a different name you will have to update the url in ajax request in timely-greeting.js so it matches the file's path.
* 2. Then add this line: include('includes/timely-greeting.js'); in your PHP code in the page where you want to use the script (inside the footer works fine).
* 3. Lastly, paste this tag where you want the greeting to display: <span class='timelyGreeting'></span>
* */
<?php
// Handle AJAX request
if( isset($_POST['value']) ){
// Storing the timezone offset from the jQuery code
$timezone_offset_minutes = $_POST['value'];
// Convert minutes to seconds
$timezone_name = timezone_name_from_abbr("", ($timezone_offset_minutes*60), false);
//JS code is needed to retrieve the user's timezone dynamically so we have it set statically for now
date_default_timezone_set($timezone_name);
// Morning start and end times 12:00AM - 11:59AM (12:00AM - 11:59AM)
$morningStart = '0000';
$morningEnd = '1159';
// Afternoon start and end times 12:00PM - 4:59PM (12:00PM - 4:59PM)
$afterNoonStart = '1200';
$afterNoonEnd = '1659';
// Evening start and end times 5:00PM - 11:59PM (5:00PM - 11:59PM)
$eveningStart = '1700';
$eveningEnd = '2359';
// The text greetings. **You can edit the values below but do not edit the keys**
$greetings = array(
"morning" => "Good morning",
"afternoon" => "Good afternoon",
"evening" => "Good evening"
);
// Retrieving the current time
$now = date('H:i');
// Removing the colon from the $now variable
$now = str_replace(":" , "" , "$now");
//Checking the current time of day in order to display the correct greeting
if ( ($now >= $morningStart) && ( $now <= $morningEnd ) ) {
echo $greetings["morning"];
} elseif ( ($now >= $afterNoonStart) && ( $now <= $afterNoonEnd) ) {
echo $greetings["afternoon"];
} elseif ( ($now >= $eveningStart) && ( $now <= $eveningEnd) ) {
echo $greetings["evening"];
}
exit;
}
var script = document.createElement('script');
script.src = '//code.jquery.com/jquery-1.11.0.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
var d = new Date();
var month = d.getMonth();
var day = d.getDate();
var dow = d.getDay();
var timezone_offset_minutes = new Date().getTimezoneOffset();
timezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes; // if the offset is 0 leave it alone or else negate the offset value (example: 100 becomes -100).
console.log( "User's local TZ offset in minutes: " + timezone_offset_minutes); // logging the user's timezone offset in min
// Checks if DST is on and subtracts 60 minutes from the UTC offset to correctly adjust for Daylight Savings
if (isDST(day, month, dow)) { // isDST returns "true"
timezone_offset_minutes = timezone_offset_minutes - 60;
console.log("DST in North America is Currently ON (Started on the 2nd Sun of Mar & will end on the 1st Sun of Nov)");
} else { // isDST returns "false"
console.log("DST in North America is Currently OFF");
}
// Function that checks if DST in North America is on or off. Returns true for on and false for off.
function isDST(day, month, dow) {
//January, february, and december are out.
if (month < 2 || month > 10) { return false; }
//April to October are in
if (month > 2 && month < 10) { return true; }
var previousSunday = day - dow; //Getting the previous sunday day by subtracting the dow from the day of month.
//In march, we are DST if our previous sunday was on or after the 8th.
if (month == 2) { return previousSunday >= 8; }
//In november we must be before the first sunday to be in dst.
//That means the previous sunday must be before the 1st.
return previousSunday <= 0;
}
function fetchdata(){
$.ajax({
type: 'post',
data: {value: timezone_offset_minutes},
datatype: 'text',
url: 'includes/ajax-greeting.php', //If you didn't store the greeting files in a folder named 'includes' change url here
success: function(data){
// Perform operation on the return value
$('.timelyGreeting').text(data) /* Using 'exit;' at the end of if( isset($_POST['value']) in timely-greeting.php*/
}
});
}
$(document).ready(function(){
fetchdata(); // executing fetchdata() first so the greeting displays when the page loads
setInterval(fetchdata, 30000); // interval set to update greeting every 30 seconds. Greeting is updated live on the page without reloading
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.