Jump to content

Variable multiplication not working as expected...


msing

Recommended Posts

Hi guys, it's been a while since I taught myself PHP, and I'm going back and noticing that I can't seem to get these variables to multiply like I want them.

 

Here's the code I'm working on:

 

<?php

$daylength = 86400; //60*60*24

$current_date = time();

$days_ago = 0;
$secs_ago = $days_ago * $daylength;
$then_date = ($current_date - $secs_ago);
$filedate = date('Y-m-d', $then_date);
$filename = "file_$filedate.html";


// Testing variables
print "
\$daylength = $daylength<br />
\$current_date = $current_date <br />
\$days_ago = $days_ago <br />
\$secs_ago = $secs_ago <br />
\$then_date = $then_date <br />
\$filedate = $filedate <br />
\$filename = $filename <br />

";

print "

<h3>Here's a list of the daily files.</h3>

Today:<br />
<br />

";

if(file_exists($filename)) {
        print("<a href='./$filename'>$filedate</a><br />");
}

print "

<br />

Previous:<br />
<br />

";

$days_ago = 1;
while(file_exists($filename)) {

// Testing variables
print "
\$daylength = $daylength<br />
\$current_date = $current_date <br />
\$days_ago = $days_ago <br />
\$secs_ago = $secs_ago <br />
\$then_date = $then_date <br />
\$filedate = $filedate <br />
\$filename = $filename <br />

";

        print("<a href='./$filename'>$filedate</a><br />");
        $days_ago += 1;
}

print "

<br />



";

?>

 

So the idea is that the loop increases $days_ago by 1 each time, and thus it is supposed to roll backwards printing a link to each previous file. But as proven by the variable tests, when $days_ago is incremented and I subsequently call $secs_ago (via $then_date via $filedate via $filename), $sec_ago always ends up as 0, thus making the loop at the end infinitely print today's file link,

 

So can someone tell me why $secs_ago, when called, does not reflect its own calculation of $daylength * $days_ago?

 

Thanks in advance,

== Matt

Link to comment
Share on other sites

I got burned by this one just the other day.  It is the whole daylight savings time garbage.

 

Try:

// Create a date object for now

$dt = date_create();

// Use a negative -1 for yesterday, -2 for two days ago, etc

date_modify($dt, $days_ago . ' days');

$filedate = date('Y-m-d', $then_date);

$filename = "file_$filedate.html";

 

 

If you are doing some sort of looping remember that each time you update $dt would be cumulative sorta....

 

<?php

// Create a date object for now

$dt = date_create();

 

// Probably wrong:  today, yesterday, 3 days ago, 6 days ago...

for ($i = 0; $i < 5; $i++) {

  date_modify($dt, '-' . $i . ' days');

  echo $dt->format('Y-m-d'), "\n";

}

 

$dt = date_create();

 

// Probably correct:  today, yesterday, 2 days ago, 3 days ago...

for ($i = 0; $i < 5; $i++) {

  date_modify($dt, '-1 days');

  echo $dt->format('Y-m-d'), "\n";

}

?>

 

Link to comment
Share on other sites

@fnairb: I might give that a try, but I'm really more concerned with why my current code doesn't work. I don't see how daylight savings time has anything to do with this. It looks to me like a simple calculation error.

 

For some reason $secs_ago (a.k.a. $days_ago * $daylength) always = 0, no matter what those values are.

 

you must recalculate your variables in loop

 

Ok, but how and why?

Link to comment
Share on other sites

while(file_exists($filename)) {
//recalculte
$secs_ago = $days_ago * $daylength;
$then_date = ($current_date - $secs_ago);
$filedate = date('Y-m-d', $then_date);
$filename = "file_$filedate.html";

// Testing variables
print "
\$daylength = $daylength<br />
\$current_date = $current_date <br />
\$days_ago = $days_ago <br />
\$secs_ago = $secs_ago <br />
\$then_date = $then_date <br />
\$filedate = $filedate <br />
\$filename = $filename <br />

";

        print("<a href='./$filename'>$filedate</a><br />");
        $days_ago += 1;
}

Link to comment
Share on other sites

Ah yes, it's all starting to come back to me now. I really wish I could just use a function to re-run the values on these variables every time I wanted the values for a different number of days ago. But as I recall using globals in conjunction with functions is a nightmare. Too bad it can't just be like Perl, where you can turn globals on and off per file.

 

Anyway, here is what I have now. It seems to be working for the most part. The first two file_exists test works, but the file_exists test in the for loop don't seem to return a bool like they're supposed to. See anything wrong?

 

<?php

$daylength = 86400; //60*60*24
$path = '/path/to/file/';
$current_date = time();

$days_ago = 0;
$secs_ago = $days_ago * $daylength;
$then_date = ($current_date - $secs_ago);
$filedate = date('Y-m-d', $then_date);
$filename = "file_$filedate.html";
$filepath = $path . $filename;
$exists = file_exists($filepath);

print "
<h3>Here's a list of the daily files.</h3>

Today:<br />
<br />
";

// Testing variables
print "
\$daylength = $daylength<br />
\$path = $path<br />
\$current_date = $current_date<br />
\$days_ago = $days_ago<br />
\$secs_ago = $secs_ago<br />
\$then_date = $then_date<br />
\$filedate = $filedate<br />
\$filename = $filename<br />
\$filepath = $filepath<br />
\$exists = $exists<br />
";

if(file_exists($filepath)) {
        print("<a href='./$filename'>$filedate</a><br />");
}

print "
<br />

Previous:<br />
<br />
";

for($i = 0; $i += 1; file_exists($filepath)) {

        $days_ago = $i;
        $secs_ago = $days_ago * $daylength;
        $then_date = ($current_date - $secs_ago);
        $filedate = date('Y-m-d', $then_date);
        $filename = "file_$filedate.html";
        $filepath = $path . $filename;
        $exists = file_exists($filepath);

        // Testing variables
        print "
        \$daylength = $daylength<br />
        \$current_date = $current_date <br />
        \$days_ago = $days_ago <br />
        \$secs_ago = $secs_ago <br />
        \$then_date = $then_date <br />
        \$filedate = $filedate <br />
        \$filename = $filename <br />
        \$filepath = $filepath <br />
        \$exists = $exists <br />
        ";

        print("<a href='./$filename'>$filedate</a><br /><br />");
}

?>

 

So the output is still looping:

 

Here's a list of the daily files.

Today:

 

$daylength = 86400

$path = /path/to/file/

$current_date = 1204065178

$days_ago = 0

$secs_ago = 0

$then_date = 1204065178

$filedate = 2008-02-26

$filename = file_2008-02-26.html

$filepath = /path/to/file/file_2008-02-26.html

$exists = 1

2008-02-26

 

Previous:

 

$daylength = 86400

$current_date = 1204065178

$days_ago = 1

$secs_ago = 86400

$then_date = 1203978778

$filedate = 2008-02-25

$filename = file_2008-02-25.html

$filepath = /path/to/file/file_2008-02-25.html

$exists =

2008-02-25

 

$daylength = 86400

$current_date = 1204065178

$days_ago = 2

$secs_ago = 172800

$then_date = 1203892378

$filedate = 2008-02-24

$filename = file_2008-02-24.html

$filepath = /path/to/file/file_2008-02-24.html

$exists =

2008-02-24

 

$daylength = 86400

$current_date = 1204065178

$days_ago = 3

$secs_ago = 259200

$then_date = 1203805978

$filedate = 2008-02-23

$filename = file_2008-02-23.html

$filepath = /path/to/file/file_2008-02-23.html

$exists =

2008-02-23

Link to comment
Share on other sites

Ok, I got it now. The loop syntax was the biggest problem. Thanks. I also remembered that I'm supposed to be a programmer or something... and started acting like it. Here's what I ended up with, it works great.

 

<?php

$daylength = 86400; //60*60*24
$path = '/path/to/file/';
$name_prefix = 'file_';
$link_path = './files/';
$current_date = time();

print "
<h3>Here's a list of the daily files.</h3>

Today:<br />
<br />
";

if (report_exists_from_x_days_ago(0)) {

        $filename = report_name_from_x_days_ago(0);
        $filedate = report_date_from_x_days_ago(0);
        $link = $link_path . $filename;
        print("<a href='$link'>$filedate</a><br /><br />");
}

print "
Previous:<br />
<br />
";

for($days_ago = 1; report_exists_from_x_days_ago($days_ago); $days_ago += 1) {

        $filename = report_name_from_x_days_ago($days_ago);
        $filedate = report_date_from_x_days_ago($days_ago);
        $link = $link_path . $filename;
        print("<a href='$link'>$filedate</a><br /><br />");
}


function report_exists_from_x_days_ago($days_ago) {

        global $path;

        $filename = report_name_from_x_days_ago($days_ago);
        $filepath = $path . $filename;
        $exists = file_exists($filepath);

        return $exists;
}

function report_name_from_x_days_ago($days_ago) {

        global $name_prefix;

        $filedate = report_date_from_x_days_ago($days_ago);
        $filename = $name_prefix . $filedate . ".html";

        return $filename;
}

function report_date_from_x_days_ago($days_ago) {

        global $daylength;
        global $current_date;

        $secs_ago = $days_ago * $daylength;
        $then_date = ($current_date - $secs_ago);
        $filedate = date('Y-m-d', $then_date);

        return $filedate;
}

?>

 

Hooray for functions and well-used globals :D ... and of course proper for loop syntax :o

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.