Comparing two dates in PHP -- I can't get them to "equal" each other.

So I have a couple of simple pages.  For my example, one page passes the value for date("Ydm")*12 to another page.  The other page calculates date("Ydm")*12 as well, and if they match, then it displays something.

It's not meant to be super secure by any means, it's for a simple page that already has headers, so I can't manipulate them (no ob_start didn't work) that I need to jump to.

However, as soon as I perform an operation on the date value, though they look equal when I ECHO, they never come back equal.  Is that because dates aren't treated the same as normal strings?  

I even tried covnerting to unix time.... Ideas?


jgantesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Have you tried using strcmp() rather than ==?

-r-
0
TeRReFCommented:
Since you cast them to integers, you should be able to do:
if ($date1 - $date2 === 0)
0
ellandrdCommented:
please post the code where the comparison is made, perhaps then we can help you better
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

jgantesAuthor Commented:
Pretty simple stuff, but like I said when I pass it between pages (?ImIn=).  Like I said, if you echo the values, they look the same, but you can't compare the two (you can, but they're never equal).

$DateK = Date("Ymd");
$VerifyDK = strtotime($DateK);

if($ImIn != $VerifyDK){

//Do nothing. Display error.

}else{

//Do Something

}
0
ellandrdCommented:
PHP's time and date functions behave differently, and produce very different output.  By mutliplying each Date() output by 12, you are forcing it to become a static number type variable, so it won't change with time!  

You don't need the strtotime() function, all you need to do is compare the two dates when multiplied by 12.  This will work assuming that the date doesn't change in the time between the two scripts being executed (midnight!!):



$ImIn = Date("Ymd")*12;  //this will usually come from your other page via POST or GET methods.

$VerifyDK=Date("Ymd")*12;

if($ImIn != $VerifyDK){

//Do nothing. Display error.

}else{

//Do Something
echo "Hello world, did i make it on time ;o)";

}
0
RoonaanCommented:
Make sure that $ImIn actually exists. Shouldn't you be using $_GET['ImIn'] ?

-r-
0
jgantesAuthor Commented:
Register Globals is on... it's an internal server...
0
jgantesAuthor Commented:
Ellandrd,

That's what I was doing to start with (something very simple) but I could never seem to get them to be equal.  With that said, I started playing with other ideas--

I'll make a couple new pages to test and take it from there I suppose :-)
0
TeRReFCommented:
Use var_dump() on all relevant vars. see if there are any difference in length, if all are set, if types are the same, etc.
0
jgantesAuthor Commented:
Terref--

That's a great idea... I don'tknow why I didn't think of that.  I wil try.
0
jgantesAuthor Commented:
Just did what you suggested and they look exactly the same.  I can't figure this out....

Results of Var_Dump from each page:

float(199678444583)

float(199678444583)


0
RoonaanCommented:
Can't you share sessions among your servers? When you build sessions to use a database backend, then this should be no problem. Then you can store ip + data/key inside your session and live happily ever after.

-r-
0
jgantesAuthor Commented:
This problem all originated becasuse the of the Headers Already sent error message were were getting since the user was already logged in.  We use a download.php that sends additional headers to download files-- of course we tried OB_start, but we still go header errors.
0
jgantesAuthor Commented:
I must be doing something wrong... they appear to be the same.
0
ellandrdCommented:
jgantes,

Are you able to post the small sections of code, first from the sending page, and then the receiving page, where the two numbers are compared.  I'm fairly confident that my solution should work, even if other page errors exist.  I can edit your existing code for you if you post it.  Don't post the entire code, just all the code that deals with the posting/transferring the first date() data, and the code that receives it and compares it.

It'll be much easier to help if i run the small sample code on my own machine!

0
ellandrdCommented:
Have you fixed yoru issue?

i have tested this and this works for me comparing the dates..

index.php

<?php
$strDate1 = '';
$strDate1 .= date('Ydm')*12;

header('Location: index2.php?date1='.$strDate1);
exit;
?>

index2.php

<?php
$strDate1 = $strDate2 = '';

if(isset($_GET['date1'])){
      $strDate1 .= $_GET['date1'];
}
$strDate2 .= date('Ydm') * 12;

echo 'Date 1: '.$strDate1.'<br/>Date 2: '.$strDate2.'<br/>';

if($strDate1 === $strDate2)
      echo 'Dates are same';
else
      echo 'Dates are different';      
?>

ellandrd
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgantesAuthor Commented:
Sorry, I've been out on travel.  I'll be trying soon... I'm pretty sure I did something wrong,
0
ellandrdCommented:
thanks for the points and grade!

ellandrd
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.