This Code is Driving me CRAZY!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bmb
Forum Newbie
Posts: 2
Joined: Fri Feb 29, 2008 10:04 am

This Code is Driving me CRAZY!!!

Post by bmb »

A bit of history... First the earth cooled... then the dinosaurs came... Fast forward a few million years.

I'm responsible for creating and maintaining a set of metrics used at my company. These were originally done in excel *shudders*. I eventually saw the light and bought a book on PHP and MySQL. This, as you can imagine, has greatly simplified my life. However, I'm running into a very strange situation.

The following code generates different results when comparing runs in a terminal window and a browser. I have, for days now, tried to figure out what's going on... I've finally resolved to asking the experts for help.

Here's the sample test case I've created:

Code: Select all

 
<?php
 
    class metricInfo {
        var $weekEnd;
    }
 
    // setup today
    $epoc = gettimeofday();
    $today = date('Y-m-d', $epoc['sec']);
 
    // print html header
    print "<html>\n";
    print "<head>\n";
    print"<title>Macallan Bug Metrics v1.0</title>\n";  
    print "</head>\n";
    print "<body>\n";
 
    // just confirm the time...
    print "Today is: $today<br>\n";
 
    // get two new instances
    $thisWeek = new metricInfo;
    $prevWeek = new metricInfo;
 
    // setup today
    $thisWeek->weekEnd = $today;
 
    for ($i= 0; $i < 4; $i++){
 
        // tell me what last week was
        $date = $epoc['sec'] - (86400 * (7 * ($i+1)));
        $prevWeek->weekEnd = date('Y-m-d', $date);
        print("debug: i = $i thisWeek: $thisWeek->weekEnd prevWeek: $prevWeek->weekEnd<br>\n");
 
        // last week is now the current week... 
        $thisWeek = $prevWeek;
        print("debug: i = $i thisWeek: $thisWeek->weekEnd prevWeek: $prevWeek->weekEnd<br><br>\n");
    }
 
    print "</body>\n";
    print "</html>\n";
 
?>
 
Here's the results I get in a terminal window:

Code: Select all

Today is: 2008-02-29<br>
debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22<br>
debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22<br><br>
debug: i = 1 thisWeek: 2008-02-22 prevWeek: 2008-02-15<br>
debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15<br><br>
debug: i = 2 thisWeek: 2008-02-15 prevWeek: 2008-02-08<br>
debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08<br><br>
debug: i = 3 thisWeek: 2008-02-08 prevWeek: 2008-02-01<br>
debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01<br><br>
 
Here's what the output is from a browser:

Code: Select all

 
Today is: 2008-02-29
debug: i = 0 thisWeek: 2008-02-29 prevWeek: 2008-02-22
debug: i = 0 thisWeek: 2008-02-22 prevWeek: 2008-02-22
 
debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15
debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15
 
debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08
debug: i = 2 thisWeek: 2008-02-08 prevWeek: 2008-02-08
 
debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01
debug: i = 3 thisWeek: 2008-02-01 prevWeek: 2008-02-01
 
It's as if the object copy is treated as an address copy in a browser and just as a copy in a terminal...

Any thoughts or advice... This is killing me.

Thanks.
Brian
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: This Code is Driving me CRAZY!!!

Post by hawkenterprises »

I my be a fool but isn't the difference is your seeing the source in a terminal and your seeing the compiled source in a web browser, that's the only difference I really see.
bmb
Forum Newbie
Posts: 2
Joined: Fri Feb 29, 2008 10:04 am

Re: This Code is Driving me CRAZY!!!

Post by bmb »

Nope.. the output is actually different...

Let's boil it down to a single example...

From the terminal, on the second iterations I get:
(pre comparison, top of the loop where I generate prev)
# debug: i = 1 thisWeek: 2008-02-22 prevWeek: 2008-02-15
(post comparison, where I assign $this = $prev)
# debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15

From the browser I get:
(pre comparison, top of the loop where I generate prev)
# debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15
(post comparison, where I assign $this = $prev)
# debug: i = 1 thisWeek: 2008-02-15 prevWeek: 2008-02-15

If you look at the first line, the terminal is reporting this week as 2/22 and prev as 2/15. The browser says BOTH this week and prev week are 2/15. In other words, in the real application, I'm getting a report every week. In the browser I get every other week, however this screws up the calculations since it's doing a comparison between the two weeks. My example, of course, excludes those comparisons just to show that the output is different.
Post Reply