任何人都可以推断如何实现基于用户的时间设定一个基本的“晚上好”或“早上好”?设置基于用户的时间问候(早上好,下午好...)
也许PHP将获取服务器的时间,但我期待与他们认为一天中的时间基于时间的合适的问候迎接站点访问者。
例如为:早上好,晚上好,下午好。
任何人都可以推断如何实现基于用户的时间设定一个基本的“晚上好”或“早上好”?设置基于用户的时间问候(早上好,下午好...)
也许PHP将获取服务器的时间,但我期待与他们认为一天中的时间基于时间的合适的问候迎接站点访问者。
例如为:早上好,晚上好,下午好。
它的基础上的日期对象的.getHours()
。使用JavaScript的Date对象将自动使用用户的本地时间,而不是服务器时间:
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
<?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。我希望这将有所帮助。
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答案= '咖啡'
STORE用户在应答变量输入
IF答案= '否' THEN
OUTPUT ‘No problem, I will just get that for you!’
ELSE
IF答案=“是” THEN
OUTPUT ‘No problem, I will take care of that for you!’
Make coffee for customer
<?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";
}
?>
$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;
记得有一个后备的情况下的时间不能被检测到或者当JavaScript被禁用,例如预设'msg ='欢迎'。 – Gordon 2010-02-02 08:09:14
当javascript被禁用时,你不需要'msg ='Welcome''因为没有javascript – Natrium 2010-02-02 08:13:09
@Gordon:正确。这个例子绝不仅仅是一个例子。 – Sampson 2010-02-02 08:13:37