7

I'm building a simple date verification script and I just stumbled onto 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 needs to enter the date in which they placed the order. But clearly, this is never going to work with the way I’ve setup my script. The location of the business is -2 hours from the server. But that doesn't matter since someone can place the order anywhere with in the United States. So if they are in New York, it could be the next day, and the business being in LA, the dates would differ. Also It could be the last day of the month and the month in New York would differ as well from LA.

The only way around this is in my head is to build an entire if/else nested set of rules to adjust for time and month difference at specific times. But I'm almost sure there has to be another way around this that I'm not aware of.

Any suggestions?

laurent
  • 88,262
  • 77
  • 290
  • 428
Outdated Computer Tech
  • 1,996
  • 4
  • 25
  • 39

3 Answers3

3

I would opt to add a timezone dropdown and attempt to select the correct one for them using JS so that you can sleep easy knowing that it is being provided to you from the client-side.

Upon receiving the data on your server you should either use this information to convert the user-date to the server's timezone and compare them or create a a server-date value which is in the user-supplied timezone and check against that.

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);
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
2

You need to know the user time zone in order to do this. Either you ask them to input it (reliable but not user-friendly) or you use the GeoIP lib. However GeoIP is less reliable since the users might be behind a VPN or proxy that might hide their IP.

Perhaps you could also do the validation on the JS-side. That would be reliable (assuming the user's system clock is correct) but obviously it's not secure, so it depends on your use-case.

laurent
  • 88,262
  • 77
  • 290
  • 428
2

How can I obtain the local time in jQuery?

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

Community
  • 1
  • 1
greendave11
  • 168
  • 1
  • 13