Link to home
Start Free TrialLog in
Avatar of xenoula
xenoula

asked on

Problem with comparing dates

Hello everyone,

I have problem when I am comparing dates.More specific I want to find in which period somebody would like to rent a car.If the date that the user select is between 01/04/2006 and 15/06/2006 it means that it is in the first period and will assign in the variable $pickupperiod=1;. The problem is that it doesn't compare them.Actually it goes through all the if statements instread of taking only the correct period.


The following code is a part of what I am using:
PHP Code:
    $period[1][1]="01/04/2006";
    $period[1][2]="15/06/2006";
    $period[2][1]="16/06/2006";
    $period[2][2]="15/07/2006";
    $period[3][1]="16/07/2006";
    $period[3][2]="31/08/2006";
    $period[4][1]="01/09/2006";
    $period[4][2]="30/09/2006";
    $period[5][1]="01/10/2006";
    $period[5][2]="31/10/2006";

$pickupdate= $_POST["pickupdate"];


if (($period[1][1] < $pickupdate) && ($pickupdate < $period[1][2]))

        {
            $pickupperiod=1;
           
        }

if (($period[2][1] < $pickupdate) && ($pickupdate < $period[2][2]))
        {
            $pickupperiod=2;
        }

if (($period[3][1] < $pickupdate) && ($pickupdate < $period[3][2]))
            {
                  $pickupperiod=3;
            }
if (($period[4][1] < $pickupdate) && ($pickupdate < $period[4][2]))
            {
                  $pickupperiod=4;
            }
            
if (($period[5][1] < $pickupdate) && ($pickupdate < $period[5][2]))
            {
                  $pickupperiod=5;
            }

if (($period[1][1] < $dropoffdate) && ($dropoffdate < $period[1][2]))
            {
                  $dropoffperiod=1;
            }

if (($period[2][1] < $dropoffdate) && ($dropoffdate < $period[2][2]))
            {
                  $dropoffperiod=2;
            }

if (($period[3][1] < $dropoffdate) && ($dropoffdate < $period[3][2]))
            {
                  $dropoffperiod=3;
            }

if (($period[4][1] < $dropoffdate) && ($dropoffdate < $period[4][2]))
            {
                  $dropoffperiod=4;
                  
            }

if (($period[5][1] < $dropoffdate) && ($dropoffdate < $period[5][2]))
            {
                  $dropoffperiod=5;
                  
            }

if ($pickupperiod-$dropoffperiod==0) {

$days[$pickupperiod]= datediff(d, $dropoffdate, $pickupdate, $dropoffhour, $pickuphour );
            
            echo "days[$pickupperiod]= ". $days[$pickupperiod];
            echo "dropoffdate:  ". $dropoffdate;
      }

if ($dropoffperiod-$pickupperiod==1) {


$days[$pickupperiod]= datediff(d,$period[$pickupperiod][2], $pickupdate, $period_time,$pickuphour);
$days[$dropoffperiod]=datediff(d,$dropoffdate, $period[$dropoffdate][1], $dropoffhour,$period_time);
            echo " \n". $days[$pickupperiod];
            echo "\n ". $days[$dropoffperiod];
      }

if ($dropoffperiod-$pickupperiod==2) {

            $days[$pickupperiod]= ($period[$pickupperiod][2] - $pickupdate);
            $days[$dropoffperiod]= ($dropoffdate-$period[$dropoffdate][1]);
            $days[$pickupdate+1]= ($period[$pickupdate+1][2] - $period[$pickupdate+1][1]);
            
            echo "days[$pickupperiod]= ". $days[$pickupperiod];
            echo "days[$dropoffperiod]= ". $days[$dropoffperiod];
            
      }
      

$a[0]=$days[1]+$days[5];
$b[0]=$days[2]+$days[4];
$c[0]=$days[3];


Could you please help me is there is any specific function in php that is doing the comparison or any other idea?

Thank you in advance,
Xenia
Avatar of TeRReF
TeRReF
Flag of Netherlands image

You are comparing strings instead of dates.
You should convert the dates to unix_timestamps (which are integers) and use those for your if statements...
Have a look at:
http://php.net/manual/en/function.mktime.php

If you need an example, let me know...

Avatar of xenoula
xenoula

ASKER

Can I use the function mktime to take only dd mm and yyyy and not the second,minutes and hours?

Can you please provide an example regarding my code?

Thank you very much,
Xenia
ASKER CERTIFIED SOLUTION
Avatar of TeRReF
TeRReF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use mktime and give 0's for the seconds minutes and hours. www.php.net/mktime
Or just use StrToTime() www.php.net/strtotime

e.g.
  $period[1][1]="01/04/2006";
//...

$pickupdate= $_POST["pickupdate"];

if ((StrToTime($period[1][1]) < StrToTime($pickupdate)) && (StrToTime($pickupdate) < StrToTime($period[1][2])))  {
     $pickupperiod=1;
}


... it's more efficient to do the conversions once ....

  $period[1][1]= StrToTime("01/04/2006");
    $period[1][2]= StrToTime("15/06/2006");
//...

$pickupdate= StrToTime($_POST["pickupdate"]);

// leave remainder of code unchanged...

.... or, using mktime ....

  $period[1][1]= MkTime(0, 0, 0, 4, 1, 2006);
  $period[1][2]= MkTime(0, 0, 0, 6, 15, 2006);


For your date calcultions towards end of script - other split up all your times using GetDate() www.php.net/getdate -- or use StrToTime which can take +'s & -'s, such as "$date + 1 day" -- or you may like to use a combination

This gives you several options, all of which should work fine.
Avatar of xenoula

ASKER

I used TeRReF code and also I modify  some parts of the code and now is partly working. What I mean with partly is that when I tryied to run
the page and select from the form as pick up date 10/05/2006 and drop off date 12/05/2006(low period)it gave me the correct results as it entered in the correct if statement(the first one).However when I tryied to put dates from the high season ex.pick up date  05/08/2006 and drop off date 08/08/2006 I had problem. It didn't enter to the correct if statement and didn't print the correct period $pickuperiod and %dropoffperiod variables.

Could you please have a look If you can see any error to my code?

      $period[1][1]=mktime(0,0,0,04,01,2006);
      $period[1][2]=mktime(0,0,0,06,15,2006);
      $period[2][1]=mktime(0,0,0,06,16,2006);
      $period[2][2]=mktime(0,0,0,07,15,2006);
      $period[3][1]=mktime(0,0,0,07,16,2006);
      $period[3][2]=mktime(0,0,0,08,31,2006);
      $period[4][1]=mktime(0,0,0,09,01,2006);
      $period[4][2]=mktime(0,0,0,09,30,2006);
      $period[5][1]=mktime(0,0,0,10,01,2006);
      $period[5][2]=mktime(0,0,0,10,31,2006);

$pickupperiod=0;
$dropoffperiod=0;

$days[1]=0;
$days[2]=0;
$days[3]=0;
$days[4]=0;
$days[5]=0;

$a[0]=0;
$b[0]=0;
$c[0]=0;

      $pickupdate= $_POST["pickupdate"];
      echo "\n pick up user: " .$pickupdate;
    $pd = explode('/', $pickupdate);
      $pickuphour= $_POST["pickuphours"];
    $ph = explode(':', $pickuphour);
      $newpickupdate = mktime($ph[0], $ph[1], 0, $pd[1], $pd[0], $pd[2]);
      
      $dropoffdate= $_POST["dropoffdate"];
      echo "\n drop off user: " .$dropoffdate;
    $dd = explode('/', $dropoffdate);
      $dropoffhour= $_POST["dropoffhours"];
    $dh = explode(':', $dropoffhour);
    $newdropoffdate = mktime($dh[0], $dh[1], 0, $dd[1], $dd[0], $dd[2]);

if (($period[1][1] < $newpickupdate) && ($newpickupdate < $period[1][2]))
            {
                  $pickupperiod=1;
                  echo "mpike period 1 \n";

            }

if (($period[2][1] < $newpickupdate) && ($newpickupdate < $period[2][2]))
            {
                  $pickupperiod=2;
                  echo "mpike period 2";
            }      

if (($period[3][1] < $newpickupdate) && ($newpickupdate < $period[3][2]))
            {
                  $pickupperiod=3;
                  echo "mpike period 3";
            }      
            
if (($period[4][1] < $newpickupdate) && ($newpickupdate < $period[4][2]))
            {
                  $pickupperiod=4;
                  echo "mpike period 4";

            }
            
if (($period[5][1] < $newpickupdate) && ($newpickupdate < $period[5][2]))
            {
                  $pickupperiod=5;
                  echo "mpike period 5";
            }

if (($period[1][1] < $newdropoffdate) && ($newdropoffdate < $period[1][2]))
            {
                  $dropoffperiod=1;
                  echo "mpike drop off period 1";
            }

if (($period[2][1] < $newdropoffdate) && ($newdropoffdate < $period[2][2]))
            {
                  $dropoffperiod=2;
                  echo "mpike drop off period 2";
            }

if (($period[3][1] < $newdropoffdate) && ($newdropoffdate < $period[3][2]))
            {
                  $dropoffperiod=3;
                  echo "mpike drop off period 3";
            }

if (($period[4][1] < $newdropoffdate) && ($newdropoffdate < $period[4][2]))
            {
                  $dropoffperiod=4;
                  echo "mpike drop off period 4";
            }

if (($period[5][1] < $newdropoffdate) && ($newdropoffdate < $period[5][2]))
            {
                  $dropoffperiod=5;
                  echo "mpike drop off period 5";
            }

if ($pickupperiod-$dropoffperiod==0) {


$days[$pickupperiod]=(   ($newdropoffdate-$newpickupdate)/3600  )/24;

      }

if ($dropoffperiod-$pickupperiod==1) {

$days[$pickupperiod]=( ($period[$pickupperiod][2] - $newpickupdate)/3600 ) /24;
$days[$dropoffperiod]=( ($newdropoffdate-$period[$newdropoffdate][1])/3600 ) /24;

      }

if ($dropoffperiod-$pickupperiod==2) {

            $days[$pickupperiod]=( ($period[$pickupperiod][2] - $newpickupdate)/3600 )/24;
            $days[$dropoffperiod]=( ($newdropoffdate-$period[$newdropoffdate][1])/3600 )/24;
            $days[$pickupdate+1]=( ($period[$newpickupdate+1][2] - $period[$newpickupdate+1][1])/3600 )/24;
            
            
      }
      


$a[0]=$days[1]+$days[5];
$b[0]=$days[2]+$days[4];
$c[0]=$days[3];

echo "a: " . $a[0];
echo "\n b: ". $b[0];
echo "\n c: ". $c[0];
echo "\n pickupperiod: " . $pickupperiod;
echo "\n dropoffperiod: ". $dropoffperiod;

Thank you in advance,
Xenia
Change this:
     $period[1][1]=mktime(0,0,0,04,01,2006);
     $period[1][2]=mktime(0,0,0,06,15,2006);
     $period[2][1]=mktime(0,0,0,06,16,2006);
     $period[2][2]=mktime(0,0,0,07,15,2006);
     $period[3][1]=mktime(0,0,0,07,16,2006);
     $period[3][2]=mktime(0,0,0,08,31,2006);
     $period[4][1]=mktime(0,0,0,09,01,2006);
     $period[4][2]=mktime(0,0,0,09,30,2006);
     $period[5][1]=mktime(0,0,0,10,01,2006);
     $period[5][2]=mktime(0,0,0,10,31,2006);

To this:
     $period[1][1]=mktime(0,0,0,4,1,2006);
     $period[1][2]=mktime(0,0,0,6,15,2006);
     $period[2][1]=mktime(0,0,0,6,16,2006);
     $period[2][2]=mktime(0,0,0,7,15,2006);
     $period[3][1]=mktime(0,0,0,7,16,2006);
     $period[3][2]=mktime(0,0,0,8,31,2006);
     $period[4][1]=mktime(0,0,0,9,1,2006);
     $period[4][2]=mktime(0,0,0,9,30,2006);
     $period[5][1]=mktime(0,0,0,10,1,2006);
     $period[5][2]=mktime(0,0,0,10,31,2006);
Avatar of xenoula

ASKER

TeRReF  thank you very much you were right , now it is working.
General the format  for the mktime is without extra 0's?

So the format is ex. $period[1][1]=mktime(0,0,0,4,1,2006);

Thank you,
Xenia
Yep, that's right.
Avatar of xenoula

ASKER

Thank again
Avatar of xenoula

ASKER

Sorry but I would like to ask you something else regarding the mktime.
When I tryied dates that are at the end of the high seasson it gave me again error.

I suppose that the error must be in the function that the user select the pick up and drop off date
as the format takes 00's.

How I can remove any 0's from the function

     $pickupdate= $_POST["pickupdate"];
     echo "\n pick up user: " .$pickupdate;
    $pd = explode('/', $pickupdate);
     $pickuphour= $_POST["pickuphours"];
    $ph = explode(':', $pickuphour);
     $newpickupdate = mktime($ph[0], $ph[1], 0, $pd[1], $pd[0], $pd[2]);
     
     $dropoffdate= $_POST["dropoffdate"];
     echo "\n drop off user: " .$dropoffdate;
    $dd = explode('/', $dropoffdate);
     $dropoffhour= $_POST["dropoffhours"];
    $dh = explode(':', $dropoffhour);
    $newdropoffdate = mktime($dh[0], $dh[1], 0, $dd[1], $dd[0], $dd[2]);

Thank you,
Xenia