这个数组保存了一周中每一天的工作时间,问题是在使用PHP之后,我意识到我无法获得客户端时间,并且我将需要JavaScript。不知道我是否可以使用一些javascript代码来获取客户当前时间?
//business hours
$storeSchedule = [
'Sun' => ['03:00 PM' => '11:00 PM'],
'Mon' => ['06:00 AM' => '11:00 PM'],
'Tue' => ['06:00 AM' => '11:00 PM'],
'Wed' => ['06:00 AM' => '11:00 PM'],
'Thu' => ['06:00 AM' => '11:00 PM'],
'Fri' => ['06:00 AM' => '09:00 PM'],
'Sat' => ['09:00 AM' => '06:00 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = time();
// default status
$status = 'closed';
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = 'open';
break;
}
}
echo "is currently $status";
答案 0 :(得分:2)
我不完全了解您的工作流程,但我会使用AJAX来执行此操作。 当页面加载时,在Javascript中获取时间,并通过AJAX将其发送到PHP脚本。 PHP将处理时间,并返回当前状态,可以再次使用JavaScript将其添加到页面
$(document).ready(function(){
var clienttime = new Date();
$.get( "your.php", {"time": clienttime}, function( data ) {
alert( "Store is: " + data );
});
});
在PHP中,您只需要更改时间的加载(也许可以解析):
$currentTime = $_GET['time']; // get the time from the get params sent by ajax
答案 1 :(得分:2)
如果我理解你,你可以用
打电话给客户 var date = new Date()
然后用AJAX发送它 Mike