我过去常常包含一些依赖类来更改时区。但如何在不添加任何依赖的情况下在PHP中实现呢?如何检查印度的当前时间[例如]是否在特定的时间范围内,并显示适当的消息
EDIT bcoz of downvotes:我自己提供了这个问题的答案,这就是我发布这个问题的原因,以便将来帮助别人。这就是为什么这个问题看起来没有任何努力/没有代码的原因,正如我在我的回答中解释的那样。
<?php
date_default_timezone_set('Asia/Calcutta');
//echo date('Y-m-d h-i-s'); // prints 2015-08-25
$currentTime = time();
// if you want to check the hour only. This can also be checked via mktime(16, 0, 0)
if (((int) date('H', $currentTime)) >= 16) {
echo "current time is greater than 4pm";
} else {
echo "current time is less than 4 pm";
}
// if you want to check the hour and minute together
if($currentTime > mktime(16, 29, 0)) {
echo " We're sorry, This service is available only before 4pm";
} else {
echo "";
}
// if you want to check the hour and minute together in a range
if($currentTime < mktime(6, 0, 0) || $currentTime > mktime(16, 30, 0)) {
echo " We're sorry, This service is available only between 6 AM and 4:30 PM";
} else {
echo "";
}
// check if current date/time is greater than a particular date/time
if (new DateTime() > new DateTime("2015-08-26 16:00:00")) {
# current time is greater than 2015-08-25 16:00:00
echo "This coupon is expired.";
}
?>