This topic is locked

Countdown to a specific date / time

4/15/2008 2:33:38 PM
PHPRunner General questions
M
mmponline author

I have a date field and a time to go field in a table.
I would like to add a code snippet to the time to go field to count down the time untill the date in the date field.
IOW Date 16/04/2008 10:30 Time to go needs to display 2 days 3:15 and counting down by the min. or second.
Any suggestions appreciated

M
mmponline author 4/18/2008

Still hoping for soem response on this...

J
Jane 4/18/2008

Stephan,
use Before record added/Before record updated events to calculate value for time to go field.

Use standart date() and strtotime() PHP functions:

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.strtotime.php

M
mmponline author 4/18/2008

Jane
I need this everytime a field is displayed (list page) - not added.
The countdown must show the value of the closing date (in closing date field) minus the current date and time.

Sergey Kornilov admin 4/18/2008

Stephan,
probably you can use something like this:

http://www.theblog.ca/date-countdown-php
Found via "countdown date php" search in google.

M
mmponline author 4/18/2008

Sergey
This code is great and works fine. Please assist on what code to add to display this in a specific fieldname.
The code in question:

function countdown($event,$month,$day,$year) {

// subtract desired date from current date and give an answer in terms of days

$remain = ceil((mktime(0,0,0,$month,$day,$year) - time()) / 86400);

// show the number of days left

if ($remain > 0) {

print "<p><strong>$remain</strong> more sleeps until $event</p>";

}

// if the event has arrived, say so!

else {

print "<p>$event has arrived!</p>";

}

}
// call the function

countdown("Christmas Day", 12, 25, 2008);


If I'm right, the field name (in my case BidCloseDate) must be added in the end //call the function

countdown (the fieldname (How?));
I've tried countdown ($values["BidCloseTime"]); but values don't seem to work on listpage.
I've also added to Listpage - Before display ???

Sergey Kornilov admin 4/18/2008

Set 'View as' type of date field to 'Custom' and put your code there.

Use $value to access this field value from within the code.

M
mmponline author 4/21/2008

I'm too stupid. Get a undefined variable: Values. Please assist!

function countdown($event,$month,$day,$year) {

// subtract desired date from current date and give an answer in terms of days

$remain = ceil((mktime(0,0,0,$month,$day,$year) - time()) / 86400);

// show the number of days left

if ($remain > 0) {

print "<p><strong>$remain</strong> more sleeps until $event</p>";

}

// if the event has arrived, say so!

else {

print "<p>$event has arrived!</p>";

}

}
// call the function

countdown($values["BidCloseTime"]);
J
Jane 4/22/2008

Stephan,
where do you call countdown() function?

M
mmponline author 4/22/2008

I've right clicked on the BidCloseTime Field and chose the custom code, putting the code there.
Not sure where to "call" from. I need the result of now() - BidCloseTime to display here.

J
Jane 4/22/2008

Stephan,
there is no $values array on the list page directly.

If you use custom format use $value variable or $data array. Don't forget to declare $data array as global before:

global $data;

M
mmponline author 4/22/2008

Tried this without success:

global $value;

function countdown($event,$month,$day,$year) {

// subtract desired date from current date and give an answer in terms of days

$remain = ceil((mktime(0,0,0,$month,$day,$year) - time()) / 86400);

// show the number of days left

if ($remain > 0) {

print "<p><strong>$remain</strong> more sleeps until $event</p>";

}

// if the event has arrived, say so!

else {

print "<p>$event has arrived!</p>";

}

}
// call the function

countdown($value["BidCloseTime"]);
J
Jane 4/22/2008

Stephan,
there is no $value array on the list page, only $value variable or $data array:

countdown($value);



or > global $conn;

countdown($data["BidCloseTime"]);

M
mmponline author 4/26/2008

I'm still in the dark with this. Please assist:

global $conn;

function countdown($event,$month,$day,$year) {

// subtract desired date from current date and give an answer in terms of days

$remain = ceil((mktime(0,0,0,$month,$day,$year) - time()) / 86400);

// show the number of days left

if ($remain > 0) {

print "<p><strong>$remain</strong> more sleeps until $event</p>";

}

// if the event has arrived, say so!

else {

print "<p>$event has arrived!</p>";

}

}
// call the function

countdown($data["BidCloseTime"]);


I get a parse error.

J
Jane 4/28/2008

Stephan,
I can't check every line in your code.

First there are four parameters in the declaration of countdown function and only one parameter in the calling.

Also you need to declare $data as global array before:

global $data;

...

M
mmponline author 5/22/2008

I'm back on this issue again, still without any luck. I found thgis piese of countdown code.
How would one use it on a field's (TimeToGo) custom code or event, to show the time left to the ClosingDate.

// countdown function

// parameters: (year, month, day, hour, minute)

countdown(2010,1,1,0,0);

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

{

// make a unix timestamp for the given date

$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
// get current unix timestamp

$today = time();
$difference = $the_countdown_date - $today;

if ($difference < 0) $difference = 0;
$days_left = floor($difference/60/60/24);

$hours_left = floor(($difference - $days_left*60*60*24)/60/60);

$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);



// OUTPUT

echo "Today's date ".date("F j, Y, g:i a")."<br/>";

echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";

echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";

}


I'm desperate to get this simple action working, but I seem to be to stupid.

M
mmponline author 5/22/2008

I found this siimpler code:

$target = mktime(0, 0, 0, 2, 10, 2009);

$today = time ();

$difference =($target-$today);

$days =(int) ($difference/86400);

$value = "Our event will occur in $days days";


Tust need to be able to replace (0, 0, 0, 2, 10, 2009) with the field name (ClosingDate) that carries the Closing Date. How?
Being able to show days, hours and mins will even be better.

J
Jane 5/23/2008

Stephan,
check date() and mktime() PHP functions in the manual:

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.mktime.php

M
mmponline author 5/23/2008

The code is setup as in the manual and is working correctly. My need is to replace the mktime(0, 0, 0, 2, 10, 2009); code so that the ClosingDate is read from the database field. This is not decribed in the maual.
Something like mktime($data["ClosingDate"]);
Tried a lot of options without getting the correct results...

A
all3ssaf 7/26/2008

The code is setup as in the manual and is working correctly. My need is to replace the mktime(0, 0, 0, 2, 10, 2009); code so that the ClosingDate is read from the database field. This is not decribed in the maual.

Something like mktime($data["ClosingDate"]);
Tried a lot of options without getting the correct results...


<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=2&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=3&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=4&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=5&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=6&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=7&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=8&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=9&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=10&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=11&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=12&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=13&table=forumreplies' class='bbc_emoticon' alt=':(' /> <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=31435&image=14&table=forumreplies' class='bbc_emoticon' alt=':(' />
??????????????????????????????????????????????

J
Jane 7/28/2008

Hi,
to parse date field use date() function:

http://php.net/manual/en/function.date.php
Here is a sample:

global $data;

$year = date("Y",strtotime($data["BidCloseDate"]));

$month = date("m",strtotime($data["BidCloseDate"]));

$day = date("d",strtotime($data["BidCloseDate"]));

$hour = date("G",strtotime($data["BidCloseTime"]));

$minute = date("i",strtotime($data["BidCloseTime"]));

$second = date("s",strtotime($data["BidCloseTime"]));



And then use these variables in the countdown function.