Compare Dates on FormValidate

I have torneo_inicio (datetime) and torneo_fin (datetime) fields on my table
then, in phpmaker I did set
torneo_inicio default (‘Y-m-d’,strtotime(‘+7 day’))
torneo_fin default date (‘Y-m-d’,strtotime(‘+120 day’))
both required…
Finally in TableSpecific → Add/Copy → FormCustomValidate I’d put
$rs = $this->GetFieldValues(“FormValue”);
if (CurrentLanguageID() == ‘es’) {
if ($rs[‘torneo_inicio’] <= date (‘d-m-Y’,strtotime(‘+2 day’))){
$customError = "El torneo no puede comenzar antes de ".date (‘d-m-Y’,strtotime(‘+2 day’));
return FALSE;
}
if ($rs[‘torneo_fin’] <= $rs[‘torneo_inicio’]){
$customError = “El torneo no puede finalizar antes de comenzar”;
return FALSE;
}
}
if (CurrentLanguageID() == ‘en’) {
if ($rs[‘torneo_fin’] <= $rs[‘torneo_inicio’]){
$customError = “Tournament can’t finish before start”;
return FALSE;
}
if ($rs[‘torneo_inicio’] <= date (‘Y-m-d’,strtotime(‘+2 day’))){
$customError = "The tournament can’t start before ".date (‘Y-m-d’,strtotime(‘+2 day’));
return FALSE;
}
}
return TRUE;

But it didn’t work well…
$rs[‘torneo_fin’] <= $rs[‘torneo_inicio’] fail
So, how can I compare two dates to validate or not new records?.


in row updating I have similar… but seems working there…
if ($rsold[‘torneo_inicio’] < $rsnew[‘torneo_inicio’]) {
if (CurrentLanguageID() == ‘es’) {
$this->setFailureMessage(“Solo puedes subir la fecha de inicio, no bajarla”);
}
if (CurrentLanguageID() == ‘en’) {
$this->setFailureMessage(“You only can set a higher startdate, not lower”);
}
return FALSE;
}

If you want to compare Dates in PHP, then make sure you have already converted it by using strtotime() https://www.php.net/manual/en/function.strtotime.php

So… I can’t use $rs[‘torneo_inicio’]…
I have to strtotime($rs['torneo_inicio)) ?

FedeLopez wrote:

I have to strtotime($rs['torneo_inicio)) ?

Yes.

It didn’t work…
I mean…
torneo_inicio = 22/09/2020;

if (strtotime($rs[‘torneo_fin’]) <= strtotime($rs[‘torneo_inicio’])) {
retunr false;
}

pass on torneo_fin 01/10/2020 but fail on torneo_fin 30/09/2020.
So in some point it is comaparing only Day number…

Make sure the “torneo_inicio” field contains a valid date, too.

Note that form values are in the format you specified in the locale file or in the View Tag setting, strtotime() may not parse the format dd/mm/yyyy correctly, you’d better test.

If your locale file uses “dmY”, you can unformat the date first, e.g. UnFormatDateTime($rs[“myDateField”]), then use strtotime().

Finally… UnFormatDateTime requiere two parameters…
Date to be unformatted and format, based on description in phpfn.php
0 - Default date format
1 - Long Date (with time)
2 - Short Date (without time)
3 - Long Time (hh:mm:ss AM/PM)

16/116 - Short Date (mm/dd/yyyy) + Short Time (hh:mm[:ss])
17/117 - Short Date (dd/mm/yyyy) + Short Time (hh:mm[:ss])So it should be
UnFormatDateTime($dt, $namedformat)I did get worked using:if (strtotime(UnFormatDateTime($rs[‘torneo_fin’],2) <= date(‘Y-m-d’,strtotime(‘+14 day’)))) {
Do something…
}