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.

$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