Solved

Problem with date passing in $_GET

Posted on 2013-12-20
16
549 Views
Last Modified: 2013-12-27
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
0
Comment
Question by:Sevron
  • 6
  • 4
  • 2
  • +2
16 Comments
 
LVL 17

Expert Comment

by:bigeven2002
ID: 39731349
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?
0
 

Author Comment

by:Sevron
ID: 39731354
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
0
 
LVL 17

Expert Comment

by:bigeven2002
ID: 39731360
No worries, so then when it shows -2005, is it showing that for the ToDate or the FromDate or both?
0
 

Author Comment

by:Sevron
ID: 39731424
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
0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 39731639
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
0
 
LVL 108

Expert Comment

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

Best regards, ~Ray
0
 

Author Comment

by:Sevron
ID: 39731672
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39731678
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39731713
Here is a simple example showing how to handle an external date, and also how to get the external date into an HTML document (which can obviously contain JavaScript).
Please see http://www.laprbass.com/RAY_temp_it_wizard.php?d=Yesterday

Explanation:
Line 5 uses the ternary notation.  It looks for $_GET['d'] and if it's present it attempts to turn it into a UNIX timestamp with strtotime().  The result will be a timestamp or FALSE on failure.  If $_GET['d'] is missing, the ternary operator will return FALSE.

Line 6 tests the timestamp variable and if it evaluates FALSE, it is set to the timestamp value for the current date.

Line 7 produces the ISO-8601 standard date representation for the date value, either the date from the $_GET['d'], or if that was invalid or missing, the current date.

Line 10 begins a HEREDOC string, a useful notation when we want to embed php variables into HTML documents.  (Sidebar note: Heed the warning but don't be put off by it; HEREDOC is really very, very useful)

Line 21 incorporates the standardized date into the JavaScript.

<?php // RAY_temp_it_wizard.php
error_reporting(E_ALL);

// ACQUIRE A DATE FROM THE URL
$ts = !empty($_GET['d']) ? strtotime($_GET['d']) : FALSE;
if (!$ts) $ts = strtotime('TODAY');
$mydate = date('Y-m-d', $ts);

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<title>HTML5 Page Using GET to Pass Date to JavaScript</title>
</head>
<body>

<script>
alert('GET DATE OR DEFAULT DATE: $mydate');
</script>

</body>
</html>
HTML5;

// RENDER THE WEB PAGE
echo $htm;

Open in new window

0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 250 total points
ID: 39731790
Hello it-wizard,

I figured out the problem with your code.

Please change your script code as following.

In following code I have put ToDate and FromDate values between single quotes ''

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

In your current code when you are assigning date to javascript variable it is taking it as literal values and performing arithmetic operations.

Hope this will help you.

Thank you.

Amar Bardoliwala
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39731822
@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
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39732476
@amar_bardoliwala is correct.  Without the quotes, javascript will perform the implied arithmetic instead of viewing it as a date string.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39737196
@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/
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39737530
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39737947
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.
0
 

Author Closing Comment

by:Sevron
ID: 39741354
Thank you Ray_Paseur and amar_bardoliwala I tried both your solutions and both where valid.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now