php - 将输入的日期与当前日期进行比较并得出错误的结果,因为服务器时区与输入的时区不同

php  - 将输入的日期与当前日期进行比较并得出错误的结果,因为服务器时区与输入的时区不同

问题描述:

I have a form where the user will enter a date and time for an email campaign. As part of the form validation, I am confirming that the user enters a future date.

The current servers default time zone is UTC.

The user will enter the scheduled date and time as a string (generated by a JQuery date and time picker).

The entered string is converted to a timestamp, and then compared with the current date and time.

If the entered date & time is before the current date & time, the form is not submitted and the user is notified to re-enter a future date.

The problem is, if the user enters a future time & date and time within the next 8 hours of the current time (which is pacific time zone), the system will think it's in the past because the server time zone is UTC (which is 8 hours ahead of PST).

I tried to convert the user input to UTC using gmdate like below:

//$timeEntered is a string, like 2015/11/06 19:27:21:
$timeEntered = $_POST['date_and_time'] 
$theEnteredTimeInUnixTimestamp = strtotime ( $timeEntered );
$enteredTimeInGMT =  gmdate('Y-m-d H:i:s', $theEnteredTimeInUnixTimestamp );

but it won't convert it. Should I just add 8 hours to the entered date and time so it matches the current server time (UTC)? That would be stupid because this will change during daylight savings time.

Should I change the current server time zone to Pacific time while doing the comparison, so the time zones match? That will give unexpected results if the users are in different time zones. I'm not sure how to handle this to make it reliably compare the entered time with the current time and give a correct result.

我有一个表单,用户可以在其中输入电子邮件广告系列的日期和时间。 作为表单验证的一部分,我确认用户输入了将来的日期。 p>

当前服务器的默认时区是UTC。 p>

用户将以字符串形式输入计划的日期和时间(由JQuery日期和 时间选择器)。 p>

输入的字符串将转换为时间戳,然后与当前日期和时间进行比较。 p>

如果输入日期& 时间是在当前日期之前& 时间,表格未提交,并通知用户重新输入未来日期。 p>

问题是,如果用户输入未来时间& 在当前时间(太平洋时区)的下一个8小时内的日期和时间,系统将认为它是过去的,因为服务器时区是UTC(比PST提前8小时)。 p> \ n

我尝试使用gmdate将用户输入转换为UTC,如下所示: p>

  // $ timeEntered是一个字符串,如2015/11/06 19:  27:21:
 $ timeEntered = $ _POST ['date_and_time'] 
 $ theEnteredTimeInUnixTimestamp = strtotime($ timeEntered); 
 $ enteredTimeInGMT = gmdate('Ymd H:i:s',$ theEnteredTimeInUnixTimestamp); 
   pre> 
 
 

但它不会转换它。 我应该在输入的日期和时间中添加8小时,以便它与当前服务器时间(UTC)匹配吗? 这将是愚蠢的,因为这将在夏令时期间发生变化。 p>

我是否应该在进行比较时将当前服务器时区更改为太平洋时间,因此时区匹配? 如果用户位于不同的时区,这将产生意外的结果。 我不确定如何处理这个问题,以便可靠地将输入的时间与当前时间进行比较并给出正确的结果。 p> div>

$user_date_str = '2015/11/06 19:27:21';
$user_tz = 'America/Los_Angeles';
$server_tz = 'UTC';

// Get a DateTime object for the user entered time
$user_datetime = new DateTime($user_date_str, new DateTimeZone($user_tz) );
// Change time zone to the same as the server
$user_datetime->setTimeZone(new DateTimeZone($server_tz));

// Get a DateTime object for the current server time
$server_datetime = new DateTime();
// If the PHP timezone isn't set correctly, you can also do:
//$server_datetime = new DateTime(null, new DateTimeZone($server_tz) );

// Now compare
if ($user_datetime < $server_datetime) {
    // handle the error
}

strtotime() is not magic. You can't just throw any old date in there and expect a valid result. It looks like if you change the backslashes to hyphens though, it should work. Refer to this page for valid date formats to use in strtotime: http://php.net/manual/en/datetime.formats.compound.php

EDIT....

Actually your strtotime call is fine.. https://3v4l.org/vCsDb

I think this is what you're looking for.. http://php.net/manual/en/function.date-default-timezone-set.php

call date_default_timezone_set ('America/Los_Angeles') at the top of that page.. using the correct timezone...

Eastern ........... America/New_York
Central ........... America/Chicago
Mountain .......... America/Denver
Mountain no DST ... America/Phoenix
Pacific ........... America/Los_Angeles
Alaska ............ America/Anchorage
Hawaii ............ America/Adak
Hawaii no DST ..... Pacific/Honolulu

http://php.net/manual/en/timezones.america.php