Link to home
Start Free TrialLog in
Avatar of Sevron
Sevron

asked on

Problem with date passing in $_GET

Hi I am passing to and from dates from 1 page to another in the URL. the first page is using a javascript function to get the values from input boxes then passes across in the URL the code is below :-

              win1.attachURL("resultforms/BusinessUnitDrillDownWin.php?pkLocationID=" + pkLocationID + "&FromDate="+fromDate+"&ToDate="+toDate);

Open in new window


the toDate and fromDate are set far up the page and work perfectly. The  toDate and fromDate also pass in the URL correctly as I have checked using firebug.

The problem starts when on the BusinessUnitDrillDownWin.php page if I echo out $_GET['toDate'] or $_GET['fromDate'] on this page the value is coming out wrong, for example if the toDate is set to 20-12-2013 and I echo this out on the BusinessUnitDrillDownWin.php page it will display as -2005

I have attached a screen shot of the URL for BusinessUnitDrillDownWin page to show the date in on the URL and is the correct date and format.

Any help would be much appreciated
url-for-BusinessUnitDrillDownWin.png
Avatar of bigeven2002
bigeven2002
Flag of United States of America image

Hello,

It appears the screenshot is a bit different from the code sample you provided.

The code shows FromDate and ToDate, but the screenshot shows FromDate12 and ToDate12.

So in your PHP, which $_GET vars are you echoing?  Is this just a plain echo or are there any other functions or nested functions involved in the echo statement?  Is it both dates showing as -2005?
Avatar of Sevron
Sevron

ASKER

Hi Sorry about I was just playing about with it trying some different approaches must of forgotten to swop the names back. I can confirm they are called FromDate and ToDate again
No worries, so then when it shows -2005, is it showing that for the ToDate or the FromDate or both?
Avatar of Sevron

ASKER

Depends if I set the date for the to variable to the same date if I do yes, if they are set to different dates the it displays a different year. It is very odd
Hello it-wizard,

it seems that it is performing subtraction of date.

20-12-2013 => -2005

if you can give your code it might help further.

Thank you.

Amar Bardoliwala
Please read this article, while I re-read your question and see if I can give you a code example that will make sense.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Best regards, ~Ray
Avatar of Sevron

ASKER

Hi the code I sent is the only relivent piece really as the date is correct in that URL but once you try and get it on that page even in a simple echo the date is then changed

<script>
        var pkLocationID = <?php echo $_GET['pkLocationID']; ?>;
        var ToDate = <?php echo $_GET['ToDate']; ?>;
        var FromDate = <?php echo $_GET['FromDate']; ?>;
        alert(ToDate);
</script>

Open in new window


this is the code which is located on the BusinessUnitDrillDownWin page and this is were is were it goes wrong.

Below is the javascripot function which opens the BusinessUnitDrillDownWin page and send the variables in the URL the variable when alerted out at this point are working
  function BusinessUnitDrillDown(pkLocationID,year)
                {
                    //alert(pkLocationID, year);
                    dhxWins = new dhtmlXWindows();
                    dhxWins.setSkin("dhx_terrace");
                    dhxWins.enableAutoViewport(true);
                    dhxWins.attachViewportTo("parentId");

                    dhxWins.setImagePath("../../dhtmlxSuite/dhtmlxWindows/codebase/imgs/");
                    // Set size for window
                    win1 = dhxWins.createWindow("win1", 0, 0, 655, 500);
                    win1.keepInViewport(false);
                    win1.centerOnScreen(true);

                    win1.setText('<p>Business Unit Incident Stats</p>'); //set window title text

                    dhxWins.window('win1').setModal(true);
                    dhxWins.window('win1').button('minmax1').hide();
                    dhxWins.window('win1').button('minmax2').hide();
                    dhxWins.window('win1').button('park').hide();

                    win1.attachURL("resultforms/BusinessUnitDrillDownWin.php?pkLocationID="+pkLocationID+"&year="+year);
                    $("html, body").animate({ scrollTop: 200 }, "slow");
                    dhxWins.attachEvent("onClose", function(win){
                        dhxWins.unload();
                    });
                }

Open in new window

First thing I see: Change the date format to use the ISO-8601 standard.  The article explains why you want to do that.
if the toDate is set to 20-12-2013 and I echo this out on the BusinessUnitDrillDownWin.php page it will display as -2005
PHP is a loosely typed language, which means that it will perform type juggling depending on the usage of the data.  If you take 20, and subtract 12, then subtract 2013, you will get -2005.  So apparently the string value was used in a contextual way that led to arithmetic.
date in on the URL and is the correct date and format
Nope, it should be in the ISO-8601 format.

I will try to put together a small example for you, but I think if you read the Date/Time article for a good understanding, you'll find the way forward.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@amar_bardoliwala: I think you have found the "dictionary picture" showing why professionals choose HEREDOC notation over the anti-pattern of flipping back and forth, in and out of PHP and HTML.  Just because you can drive on the wrong side of the road does not mean you should drive on the wrong side of the road!

Best regards, ~Ray
Avatar of Dave Baldwin
@amar_bardoliwala is correct.  Without the quotes, javascript will perform the implied arithmetic instead of viewing it as a date string.
@DaveBaldwin: Exactly, and so will PHP.  Maybe.  Or maybe not depending on context.

WTF?!  http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/
That article though it is a Whine, makes a lot of valid points.  I think he should learn .NET programming... starting with .NET 1.0 thru .NET 4.5.   Or maybe he should learn the languages like B that came before C.  There is an implication that some programming language somewhere was relatively 'perfect' the first time and that is pretty far from being true.  His beloved Python is on version 3.3.3 http://www.python.org/ .

And I stand by my statement above... except that it Always depends on the context.  I was talking about the code in the question.  I'm pretty sure I could find a context where it wasn't true.
Agreed -- it's always easier to complain about what exists than create something that is perfect in all of the details.  There are many things that do not make sense in PHP, but as he correctly points out, it's a programming language for amateur programmers.  If it were not, it would not likely be used to power millions of web sites.
Avatar of Sevron

ASKER

Thank you Ray_Paseur and amar_bardoliwala I tried both your solutions and both where valid.