Solved

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

Posted on 2006-10-26
18
283 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
Comment Utility
Have you tried using strcmp() rather than ==?

-r-
0
 
LVL 29

Expert Comment

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

Expert Comment

by:ellandrd
Comment Utility
please post the code where the comparison is made, perhaps then we can help you better
0
 

Author Comment

by:jgantes
Comment Utility
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
Comment Utility
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
Comment Utility
Make sure that $ImIn actually exists. Shouldn't you be using $_GET['ImIn'] ?

-r-
0
 

Author Comment

by:jgantes
Comment Utility
Register Globals is on... it's an internal server...
0
 

Author Comment

by:jgantes
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:jgantes
Comment Utility
Terref--

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

Author Comment

by:jgantes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I must be doing something wrong... they appear to be the same.
0
 
LVL 16

Expert Comment

by:ellandrd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the points and grade!

ellandrd
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now