Form Validation Period

I am building a simple date check script, and I just stumbled upon a big problem.

$correct_date = FALSE; if ($legal_month == $month_wozero || $legal_month == $month_wzero) { if ($legal_day == $day_wzero || $legal_day == $day_wozero) { if ($legal_year == $year) $correct_date = TRUE; } } 

$ legal_day, $ legal_month, $ legal_year = user input

$ day_wozero / wzero, $ month_wozero / wzero, $ year = server time

The user must enter the date on which they placed the order. But it is clear that this will never work with the way Ive configured my script. Location of the company - 2 hours from the server. But that doesn’t matter, as someone can place an order anywhere in the United States. Therefore, if they are in New York, it may be the next day, and the business in Los Angeles, the dates will be different. It may also be the last day of the month, and the month in New York will be different from Los Angeles.

The only way that stands in my head is to build a complete set of if / else rules to adjust the time and difference at a specific time. But I'm pretty sure there should be a different way around this that I don't know about.

Any suggestions?

+7
date timezone php forms
source share
3 answers

I would prefer to add a drop-down list of time zones and try to choose the right one for them using JS so that you can sleep peacefully, knowing that it is provided to you from the client side.

After receiving the data on your server, you must either use this information to convert the user date to the server’s time zone, or compare them or create the server date value, which is in the time zone provided by the user, and check for this.

Example

 // We'll want this for later $server_timezone = date_default_timezone_get(); // Did the user supply a timezone and is it valid? if( $_POST['user_supplied_timezone'] !== '' && date_default_timezone_set($_POST['user_supplied_timezone']) === TRUE ) { // This accepts yyyy/m/d or yyyy/mm/dd formats if( $_POST['user_year'] === date('Y') && ($_POST['user_month'] === date('n') || $_POST['user_month'] === date('m')) && ($_POST['user_day'] === date('j') || $_POST['user_day'] === date('d')) ) { // TRUE } } // It is officially "later" date_default_timezone_set($server_timezone); 
+3
source share

To do this, you need to know the user's time zone. Either you ask them to enter it (reliable, but not convenient), or you use the GeoIP lib. However, GeoIP is less reliable because users can be behind a VPN or proxy server that can hide their IP address.

Perhaps you can also do a check on the JS side. This would be reliable (assuming the user system clock is correct), but obviously it is not protected, so it depends on your use case.

+2
source share

How to get local time in jQuery?

You can always get the local time and date debate on the client slide and enter the current time indicated in the hidden input type somewhere in the form.

+1
source share

All Articles