Настройка приветствия на основе пользовательского времени (Доброе утро, добрый день...)
Может ли кто-нибудь экстраполировать, как реализовать базовый "хороший вечер" или "доброе утро" на основе настройки времени пользователя?
Возможно, PHP будет использовать время сервера, но я хочу приветствовать посетителя сайта подходящим по времени подходящим приветствием, которое учитывает их время суток.
Э.Г.: доброе утро, спокойная ночь, добрый день.
Ответы
Ответ 1
Настройте его на .getHours()
объекта даты. Использование объекта даты javascript будет автоматически использовать локальное время пользователя, а не время сервера:
var now = new Date();
alert( now.getHours() );
Пара условных проверок, и вы находитесь в бизнесе. Например, следующий пример очень простой и понятный:
var now = new Date();
var hrs = now.getHours();
var msg = "";
if (hrs > 0) msg = "Mornin' Sunshine!"; // REALLY early
if (hrs > 6) msg = "Good morning"; // After 6am
if (hrs > 12) msg = "Good afternoon"; // After 12pm
if (hrs > 17) msg = "Good evening"; // After 5pm
if (hrs > 22) msg = "Go to bed!"; // After 10pm
alert(msg);
В настоящее время 2:56 здесь, поэтому я вижу "Солнечное солнце Морнина!". когда я запускаю это. Вы можете проверить свое местное время с помощью этой онлайн-демонстрации: http://jsbin.com/aguyo3/edit
Ответ 2
<?php
// I'm India so my timezone is Asia/Calcutta
date_default_timezone_set('Asia/Calcutta');
// 24-hour format of an hour without leading zeros (0 through 23)
$Hour = date('G');
if ( $Hour >= 5 && $Hour <= 11 ) {
echo "Good Morning";
} else if ( $Hour >= 12 && $Hour <= 18 ) {
echo "Good Afternoon";
} else if ( $Hour >= 19 || $Hour <= 4 ) {
echo "Good Evening";
}
?>
Для получения дополнительной информации о дате см. Function.date. Я надеюсь, это поможет.
Ответ 3
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
$time = date("H");
/* Set the $timezone variable to become the current timezone */
$timezone = date("e");
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening";
} else
/* Finally, show good night if the time is greater than or equal to 1900 hours */
if ($time >= "19") {
echo "Good night";
}
?>
Ответ 4
$hour = date('H');
if ($hour >= 20) {
$greetings = "Good Night";
} elseif ($hour > 17) {
$greetings = "Good Evening";
} elseif ($hour > 11) {
$greetings = "Good Afternoon";
} elseif ($hour < 12) {
$greetings = "Good Morning";
}
echo $greetings;
Ответ 5
REPEAT
IF time is after 12pm THEN
OUTPUT ‘Good Afternoon, what can I get you?’
IF time is before 12pm THEN
OUTPUT ‘Good morning, what can I get you?’
INPUT user inputs what they would like
STORE the user’s input in the answer variable
IF answer = ‘Coffee’ THEN
OUTPUT ‘Would you like milk or sugar with that?
ELSE
OUTPUT ‘No I am sorry, we only serve coffee!’
UNTIL answer = 'Coffee
INPUT user inputs response
ЗАПИСЬ пользователей, входящих в переменную ответа
IF answer = 'No THEN
OUTPUT ‘No problem, I will just get that for you!’
ИНАЧЕ
IF answer = 'Yes THEN
OUTPUT ‘No problem, I will take care of that for you!’
Make coffee for customer
Ответ 6
const now = new Date().getHours();
switch (true) {
case (now<=12): console.log("Good Morning");break;
case (now>12 && now<16): console.log("Good Afternnon");break;
case (now>=16 && now<20): console.log("Good Evening");break;
case (now>=20 && now<=24): console.log("Good Night");break;}
Этот код основан на JavaScript, надеюсь, это поможет вам.