Jump to content

I can't figure out this bug in my script


Jap

Recommended Posts

Yesterday I created a script to make dates count up on an image

 

The bug happens when you select a future date that hasn't happened yet, you get negative numbers.  I can't seem to figure out how, when you select a  future date, to have it count down until it gets to that date, then count back up.  For example, have December 25th 2007 be something like "0 years 9 months & X days to go".

 

Instead what happens if you pick December 25th 2007 is: "-1 year 3 months & 5 days".

 

Here's the code for the countup script

 

 

<?php
$base_day = $_GET['date'];
$base_mon = $_GET['month'];
$base_yr  = $_GET['year'];

$current_day = date ("j");
$current_mon = date ("n");
$current_yr  = date ("Y");
$base_mon_max = date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));
$base_day_diff = $base_mon_max - $base_day;
$base_mon_diff = 12 - $base_mon - 1;
$start_day = 1;
$start_mon = 1;
$start_yr  = $base_yr + 1;
$day_diff = ($current_day - $start_day) + 1;
$mon_diff = ($current_mon - $start_mon) + 1;
$yr_diff  = ($current_yr - $start_yr);
$day_diff = $day_diff + $base_day_diff;
$mon_diff = $mon_diff + $base_mon_diff;
if ($day_diff >= $base_mon_max) {
$day_diff = $day_diff - $base_mon_max;
$mon_diff = $mon_diff + 1;
}
if ($mon_diff >= 12) {
$mon_diff = $mon_diff - 12;
$yr_diff = $yr_diff + 1;
}
$years = " Years ";
$days = " Days";
$months = " Months & ";

if ($yr_diff == "1") $years = "year";
if ($day_diff == "1") $days = "day";
?>

Link to comment
https://forums.phpfreaks.com/topic/44981-i-cant-figure-out-this-bug-in-my-script/
Share on other sites

What the code is Doing Is Correct

<?php
...................
$base_day_diff = $base_mon_max - $base_day;//Difference Of Days
$base_mon_diff = 12 - $base_mon - 1;//Difference Of Months
...................
?>

But Why You Are Using $base_yr + 1 instead of only $base_yr

<?php
......................
$start_yr = $base_yr + 1;
......................................
$yr_diff = ($current_yr - $start_yr);
..............................

Yeah I know what the code is doing is correct, BUT (re-read first post about future dates)

 

Second half of the code:

 

<?php
header("Content-type: image/png");
require("ann.php");
$string = $yr_diff.$years.$mon_diff.$months.$day_diff.$days;
$im    = imagecreatefrompng("img.png");
$color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$px    = (imagesx($im) - 7.5 * strlen($string)) / 3;
imagestring($im, 13, $px, 19, $string, $color);
imagepng($im);
imagedestroy($im);
}
?>

 

Though I don't see how showing this part of the code is even relevant.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.