Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form date verification issue

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?

like image 650
Outdated Computer Tech Avatar asked Apr 30 '15 19:04

Outdated Computer Tech


2 Answers

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);
like image 68
MonkeyZeus Avatar answered Nov 02 '22 06:11

MonkeyZeus


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.

like image 42
greendave11 Avatar answered Nov 02 '22 07:11

greendave11