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
SevronAsked:
Who is Participating?
 
Amar BardoliwalaConnect With a Mentor Commented:
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
 
bigeven2002Commented:
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
 
SevronAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
bigeven2002Commented:
No worries, so then when it shows -2005, is it showing that for the ToDate or the FromDate or both?
0
 
SevronAuthor Commented:
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
 
Amar BardoliwalaCommented:
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
 
Ray PaseurCommented:
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
 
SevronAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
 
Ray PaseurCommented:
@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
 
Dave BaldwinFixer of ProblemsCommented:
@amar_bardoliwala is correct.  Without the quotes, javascript will perform the implied arithmetic instead of viewing it as a date string.
0
 
Ray PaseurCommented:
@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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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
 
SevronAuthor Commented:
Thank you Ray_Paseur and amar_bardoliwala I tried both your solutions and both where valid.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.