If using a comparison validator for comparing time fields, it doesn't validate if either time has a single-digit minute value.

Though the minutes show as double-digits in the dropdown, when it gets to the code:

elseif ($components[$entry[$count]['key']]['type'] === 'time') {
  $time = $value['hour'] . ':' . $value['minute'];

$value['minute'] is a single-digit number. This makes the call to strtotime() fail to convert it to a timestamp every time for such values.

This can be fixed by padding the single-digit minute values to two digits with a zero.

$time = $value['hour'] . ':' . str_pad($value['minute'], 2, '0', STR_PAD_LEFT);

Comments

solideogloria created an issue. See original summary.

solideogloria’s picture

Status: Active » Needs review
StatusFileSize
new641 bytes
liam morland’s picture

Title: Time Comparison Validator doesn't work for minutes 0-9 » Comparison validator doesn't work for times with minutes 0-9

  • Liam Morland committed 18f7c0f on 7.x-1.x
    Issue #3063742 by Liam Morland: When comparing times, check that 'hour'...
  • Liam Morland committed 64bd362 on 7.x-1.x authored by solideogloria
    Issue #3063742 by solideogloria, Liam Morland: Make time comparison work...
liam morland’s picture

Status: Needs review » Fixed

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

liam morland’s picture

Status: Closed (fixed) » Active

Looking at this again, I can't reproduce the problem. Both lines below output 1564146060, so leading zeros do nothing. I'm inclined to revert the commit unless someone can find a case in which it is needed.

echo var_export(strtotime('9:1'), TRUE) . "\n";
echo var_export(strtotime('9:01'), TRUE) . "\n";
solideogloria’s picture

The error only happens if 'ampm' is set.

See in an online interpreter.

If you use the "fixed" code, it will output the time, otherwise not. Setting 'ampm' to NULL will make it work for both.

solideogloria’s picture

Status: Active » Fixed
liam morland’s picture

Thanks. For the record, this returns FALSE: strtotime('9:1 AM')

  • Liam Morland committed ec6ceed on 7.x-1.x
    Issue #3063742 by Liam Morland: Test time comparison validator with...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.