This topic is locked

Date Countdown

6/27/2008 6:01:24 AM
PHPRunner General questions
M
mmponline author

I tried to get this code before, search the net but just can't get this done. I have 2 fields.

DueDate

TimeToGo
I need to add a piece of code that will subtract the DueDate from today (now()) and display the result in the TimeToGo field. I would like to display the result in Days, hours and min.
Just can't get it right. Please help!

M
mmponline author 7/1/2008

I found a code that shows the result I want in the field:

global $return;

//Any of following input can be retrieved by GET OR POST.

//Input is set for New Year event.

$year = '2008';

$month= '12';

$day = '31';

$hour = '00';

$minute = '00';

$second = '00';

//Countdown Function

function countdown($year, $month, $day, $hour, $minute, $second)

{

global $return;

global $countdown_date;

$countdown_date = mktime($hour, $minute, $second, $month, $day, $year);

$today = time();

$diff = $countdown_date - $today;

if ($diff < 0)$diff = 0;

$dl = floor($diff/60/60/24);

$hl = floor(($diff - $dl*60*60*24)/60/60);

$ml = floor(($diff - $dl*60*60*24 - $hl*60*60)/60);

$sl = floor(($diff - $dl*60*60*24 - $hl*60*60 - $ml*60));

// OUTPUT

$return = array($dl, $hl, $ml, $sl);

return $return;

}
countdown($year, $month, $day, $hour, $minute, $second);

list($dl,$hl,$ml,$sl) = $return;

$value= "Countdown ".$dl." days ".$hl." hours ".$ml." minutes ".$sl." seconds left"."\n<br>";


I now only need to replace the countdown variable with my fieldname: BidCloseDate
I've tried the following but it returns 0 days 0 months...
Where am I going wrong?

global $return;

//Any of following input can be retrieved by GET OR POST.

//Input is set for New Year event.

$start=("BidCloseDate");

//Countdown Function

function countdown($start)

{

global $return;

global $countdown_date;

$countdown_date = mktime($start);

$today = time();

$diff = $countdown_date - $today;

if ($diff < 0)$diff = 0;

$dl = floor($diff/60/60/24);

$hl = floor(($diff - $dl*60*60*24)/60/60);

$ml = floor(($diff - $dl*60*60*24 - $hl*60*60)/60);

$sl = floor(($diff - $dl*60*60*24 - $hl*60*60 - $ml*60));

// OUTPUT

$return = array($dl, $hl, $ml, $sl);

return $return;

}
countdown($start);

list($dl,$hl,$ml,$sl) = $return;

$value= "Countdown ".$dl." days ".$hl." hours ".$ml." minutes ".$sl." seconds left"."\n<br>";
J
Jane 7/2/2008

Hi,
I'm not sure what does following line mean:

$start=("BidCloseDate")
If you use this code on the "View as" settings--> Custom dialog on the Visual Editor tab declare $data array and then use it in your code:

global $data;

$start=$data["BidCloseDate"];

M
mmponline author 7/2/2008

The code works fine (no errors), but the result is always returned as:
Countdown 83 days 0 hours 0 minutes 0 seconds left
Even if I change the BidCloseDate, the result stays the same...

J
Jane 7/3/2008

Stephan,
as I see your function requres some parameters like year, month, day and others. But you use only one parameter in your code.

You need to parse your date and pass separate values to your countdown function.
I recommend you to check PHP manual and all Date functions:

http://php.net/manual/en/ref.datetime.php

M
mmponline author 7/3/2008

As I explained, I get this code working when adding the day, month, etc, parameters. My problem is that I need the code to get the data from the BidCloseDate field in the database table. All codes in the help forums you supply only works with parameters that are not related to a database field.
If one takes the code as supplied (with date, month, etc. parameters) and paste it in the custom code of the field, it displays correctly. - But, it's then set to a specific date. I need to get it to read the date from the database field and this code is PHPRunner specific, I don't think I'll find the code anywhere else, and that is why a seek your help.