if($_POST['syear']){
$compy = strtotime($_POST['syear']);
if(date("Y") <= date("Y", $compy)){
//success
$startdate = $_POST['syear'];
}
else{
$error = 6;
}
}
I have created the above code and have no idea where I have gone wrong. I am posting a string from a form with a number in it and want to compare it to the current year. If the number is equal to or less than the current year it is supposed to be a success. It is always a success even if the number is larger than the current year. Do I need to convert some strings to ints or have I missed something entirely.
PHP handle string comparison very well, did you try this directly ? (and changing the comparison order to >=)
if($_POST['syear']){
if(date("Y") >= $_POST['syear']){
$startdate = $_POST['syear'];
}else{
$error = 6;
}
}
You could try
if(time() <= $compy){
since your already doing strtotime()
on whatever compy is originally. This way your working with 2 unix timestamps and comparing them that way.
You cannot convert simply a year to time. Using your example as is, you need to have a string of yyyy/mm/dd format to use strtotime. If you are really just checking year, you can use January 1st as a check date.
$compy = strtotime($_POST['syear'] . '-01-01' );
You can compare integers or strings the same way. If you want to be sure about the type comparison you can always cast the variable but in this case it's pointless. 2012 is inferior to 2013. "2012" is inferior to "2013". date( 'Y' ) returns a 4 characters string you can compare with your $_POST['syear'] if it's a 4 character string. Hope this helps.