Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-26
18
Medium Priority
?
333 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 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