Solved

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

Posted on 2006-10-26
18
304 Views
Last Modified: 2008-02-01
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?


0
Comment
Question by:jgantes
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17810202
Have you tried using strcmp() rather than ==?

-r-
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17810248
Since you cast them to integers, you should be able to do:
if ($date1 - $date2 === 0)
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 17810594
please post the code where the comparison is made, perhaps then we can help you better
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:jgantes
ID: 17817148
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
 
LVL 16

Expert Comment

by:ellandrd
ID: 17817752
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 17817778
Make sure that $ImIn actually exists. Shouldn't you be using $_GET['ImIn'] ?

-r-
0
 

Author Comment

by:jgantes
ID: 17819879
Register Globals is on... it's an internal server...
0
 

Author Comment

by:jgantes
ID: 17820208
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
 
LVL 29

Expert Comment

by:TeRReF
ID: 17825987
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
 

Author Comment

by:jgantes
ID: 17827106
Terref--

That's a great idea... I don'tknow why I didn't think of that.  I wil try.
0
 

Author Comment

by:jgantes
ID: 17830125
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 17830141
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
 

Author Comment

by:jgantes
ID: 17830156
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
 

Author Comment

by:jgantes
ID: 17832151
I must be doing something wrong... they appear to be the same.
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 17832288
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
 
LVL 16

Accepted Solution

by:
ellandrd earned 125 total points
ID: 17848850
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
 

Author Comment

by:jgantes
ID: 17851882
Sorry, I've been out on travel.  I'll be trying soon... I'm pretty sure I did something wrong,
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 17879514
thanks for the points and grade!

ellandrd
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question