时间戳的正确时区


Right timezone for timestamp

下面的代码将在我的网站上设置打开和关闭。但它不在正确的时区。如何将其设置为荷兰时区?

所以有人可以帮助我。

而且我还有一个关于以天为单位设置它的问题。因此,您可以根据日期在网站上设置运输时间。所以周一的发货时间是:今天发送。或者在星期天,它在星期一发送。

<?php
/**
 * Based on the following business hours:
 * (Note : I setup the hours for each day if they carry-over)
 * everyday is open from 09:00 AM - 12:00 AM
 * Sun/Sat open extra from 12:00 AM - 01:00 AM
 */
$storeSchedule = [
    'Sun' => ['00:00 AM' => '00:00 AM'],
    'Mon' => ['9:00 AM' => '00:00 PM'],
    'Tue' => ['9:00 AM' => '05:00 PM'],
    'Wed' => ['9:00 AM' => '05:00 PM'],
    'Thu' => ['9:00 AM' => '05:00 PM'],
    'Fri' => ['9:00 AM' => '05:30 PM'],
    'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = time();
// default status
$status = 'momenteel gesloten, stuur ons een <a href="/contacts/">mail</a>';
// 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 = '<b>bereikbaar op: Tel: 023-5313188</b> of <a href="/contacts/"><b>mail</b></a>';
        break;
    }
}
echo "Onze klantenservice is $status";
?>

使用它来实例化荷兰时区中的当前日期时间对象:

$current = new DateTime(
    'now',
    new DateTimeZone('Europe/Amsterdam')
);

问题出在日期时间比较上。仅从时间创建日期时间对象会将日期设置为 1970-01-01...

/**
 * Based on the following business hours:
 * (Note : I setup the hours for each day if they carry-over)
 * everyday is open from 09:00 AM - 12:00 AM
 * Sun/Sat open extra from 12:00 AM - 01:00 AM
 */
$storeSchedule = [
    'Sun' => ['00:00 AM' => '00:00 AM'],
    'Mon' => ['9:00 AM' => '00:00 PM'],
    'Tue' => ['9:00 AM' => '05:00 PM'],
    'Wed' => ['9:00 AM' => '05:00 PM'],
    'Thu' => ['9:00 AM' => '05:00 PM'],
    'Fri' => ['9:00 AM' => '05:30 PM'],
    'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = '2015-03-12 12:00:00'; // shows it is open
//$timestamp = '2015-03-12 8:59:59'; // shows closed
//$timestamp = '2015-03-12 9:00:00'; // shows open
//$timestamp = 'now'; // shows current status
// default status
$status = 'momenteel gesloten, stuur ons een <a href="/contacts/">mail</a>';
// default timezone
$timeZone = new DateTimeZone('Europe/Amsterdam');
// get current time object
$currentTime = new DateTime($timestamp, $timeZone);
// 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(
        'Y-m-d h:i A',
        $currentTime->format('Y-m-d') . ' ' . $startTime,
        $timeZone
    );
    $endTime = DateTime::createFromFormat(
        'Y-m-d h:i A',
        $currentTime->format('Y-m-d') . ' ' . $endTime,
        $timeZone
    );
    // check if current time is within a range
    if (($startTime <= $currentTime) && ($currentTime <= $endTime)) {
        $status = '<b>bereikbaar op: Tel: 023-5313188</b> of <a href="/contacts/"><b>mail</b></a>';
        break;
    }
}
echo "Onze klantenservice is $status";