Page 1 of 1

Comparing string based dates

Posted: Sun Jul 20, 2003 2:08 pm
by Slippy
I have a string date in the format: dd/mm/YYYY and I want to check that against the current date. I thought I could do that by using a simple comparison using the date() function.

Code: Select all

$expires = "t";
$expirydate = "07/22/2003";

function checkexpiry($expires, $expirydate)
{
 if ($expires == "f") {	return true; }
   else {
      if ($expirydate <= date('m/d/Y')) { return true; }
   }
   return false;
}
However, it doesn't return true when todays date is 07/20/2003 and the expiry is as it listed above.

Any ideas? :?: Should I convert the string date to something else and then make the comparison?

Posted: Sun Jul 20, 2003 2:54 pm
by Stoker
shuffle string parts to year-month-dat before comparison, or if no dates are older than 1970 then convert to unixtime and compare that way..

Posted: Sun Jul 20, 2003 3:02 pm
by Slippy
What I have done is the following and it works...

Modified Function:

Code: Select all

function checkexpiry($expires, $expirydate)
{
 if ($expires == "f") {	return true; }
   else {
      if ($expirydate >= date('Y-m-d')) { 
   return true; }
   }
  return false;
}
2 New conversion functions (to go back and forth between / and - format)

Code: Select all

function str2date($in){

$t = split("/",$in);

if (count($t)!=3) return -1;

if (!is_numeric($t[0])) return -1;
if (!is_numeric($t[1])) return -2;
if (!is_numeric($t[2])) return -3;
// 2003-09-16 < --  09/16/2003
return $t[2]."-".$t[0]."-".$t[1];
}

function date2str($in){
$t = split("-",$in);

if (count($t)!=3) return -1;

if (!is_numeric($t[0])) return -1;
if (!is_numeric($t[1])) return -2;
if (!is_numeric($t[2])) return -3;
// 2003-09-16 -- > 09/16/2003 

return $t[1]."/".$t[2]."/".$t[0];
}
It works fine now... thanks... btw - I needed to retain the / format because I have a javascript calendar that provides an easier way to input the data for the user (more difficult for me ;) )

...

Posted: Sun Jul 20, 2003 9:30 pm
by kettle_drum
Its because when you have it as DD/MM/YYYY and the DD starts with a 0 - like in 07/05/2003 php gets rid of that first leading zero, so it becomes 7052003 and causes havoc :)