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
xenoulaAsked:
Who is Participating?
 
TeRReFConnect With a Mentor Commented:
Sure you can...
Okay, here we go:

<?php
    $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";

    // I'm assuming $pickupdate has the same format
    $pickupdate= "17/08/2006";
    $pd = explode('/', $pickupdate);
    $newpickupdate = mktime(0, 0, 0, $pd[1], $pd[0], $pd[2]);
    $i = 1;
    foreach ($period as $p) {
        $bd = explode('/', $p[1]);
        $ed = explode('/', $p[2]);
        $newperiod[$i][1] = mktime(0, 0, 0, $bd[1], $bd[0], $bd[2]);
        $newperiod[$i][2] = mktime(0, 0, 0, $ed[1], $ed[0], $ed[2]);
        $i++;
    }

    if (($newnewperiod[1][1] < $newpickupdate) && ($newpickupdate < $newperiod[1][2])) {
        $pickupperiod=1;
    }
    if (($newperiod[2][1] < $newpickupdate) && ($newpickupdate < $newperiod[2][2])) {
        $pickupperiod=2;
    }
    if (($newperiod[3][1] < $newpickupdate) && ($newpickupdate < $newperiod[3][2])) {
        $pickupperiod=3;
    }
    if (($newperiod[4][1] < $newpickupdate) && ($newpickupdate < $newperiod[4][2])) {
        $pickupperiod=4;
    }
    if (($newperiod[5][1] < $newpickupdate) && ($newpickupdate < $newperiod[5][2])) {
        $pickupperiod=5;
    }
    print($pickupperiod);
?>
0
 
TeRReFCommented:
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...

0
 
xenoulaAuthor Commented:
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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CraigHarrisCommented:
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.
0
 
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
0
 
xenoulaAuthor Commented:
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
0
 
TeRReFCommented:
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);
0
 
xenoulaAuthor Commented:
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
0
 
TeRReFCommented:
Yep, that's right.
0
 
xenoulaAuthor Commented:
Thank again
0
 
xenoulaAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.