Author
Message
CodyG Life Cycles Becoming CPU Cycles Joined: Jan 02, 2003 Posts: 666 Location: Vancouver Island
Posted:
Sat Nov 29, 2008 6:43 pm
I'd like to use something like this in a block.
I've tried every which way I can think of to get the output into a $content var. I've even tried using wheredoc syntax.
Basically, all it is is a function? So, how would these echos be put into a $content var? Code:
countdown(2008,12,14,12,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 " ".date("F j, Y, g:i a",$the_countdown_date)."<br />";
echo " ".$days_left." days ".$hours_left." hrs ".$minutes_left." mins";
}
jakec Site Admin Joined: Feb 06, 2006 Posts: 3028 Location: United Kingdom
Posted:
Sun Nov 30, 2008 7:57 am
Try commenting out the echo's and add the $content outside of the function. For instance the first echo would be:
Code: $content .= "Today's date ".date("F j, Y, g:i a")."<br />";
jakec Site Admin Joined: Feb 06, 2006 Posts: 3028 Location: United Kingdom
Posted:
Sun Nov 30, 2008 7:58 am
Oh yeah obviously paste the code into the Sample Block.
CodyG Life Cycles Becoming CPU Cycles Joined: Jan 02, 2003 Posts: 666 Location: Vancouver Island
Posted:
Sun Nov 30, 2008 8:46 am
That was the first thing I tried. But I'll try it again.
Code: <?php
if ( !defined('BLOCK_FILE') ) {
Header("Location: ../index.php");
die();
}
//--------------------------
// author: Louai Munajim
// website:
//
// Note:
// Unix timestamp limitations
// Date range is from
// the year 1970 to 2038
//--------------------------
// countdown function
// parameters: (year, month, day, hour, minute)
$content = '';
countdown(2008,12,14,12,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 " ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
//echo " ".$days_left." days ".$hours_left." hrs ".$minutes_left." mins";
}
$content .= "Today's date ".date("F j, Y, g:i a")."<br/>";
$content .= " ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
$content .= " ".$days_left." days ".$hours_left." hrs ".$minutes_left." mins";
?>
( What are those other values i (0, and -1) in the mktime function? And why is the var order different than the function call? )
This is the result ....
Last edited by CodyG on Sun Nov 30, 2008 9:48 am; edited 1 time in total
jakec Site Admin Joined: Feb 06, 2006 Posts: 3028 Location: United Kingdom
Posted:
Sun Nov 30, 2008 8:52 am
Hmm, your image doesn't appear to be displaying.
CodyG Life Cycles Becoming CPU Cycles Joined: Jan 02, 2003 Posts: 666 Location: Vancouver Island
Posted:
Sun Nov 30, 2008 9:49 am
Hmm... it was looking fine for me. I put the image on another server. Is it working now?
Palbin Site Admin Joined: Mar 30, 2006 Posts: 2404 Location: Pennsylvania
Posted:
Sun Nov 30, 2008 12:02 pm
Variables do not come out of functions for the most part unless you return them.
The content variable in the function could have been named anything.
Code:
<?php
if ( !defined('BLOCK_FILE') ) {
Header("Location: ../index.php");
die();
}
//--------------------------
// author: Louai Munajim
// website:
//
// Note:
// Unix timestamp limitations
// Date range is from
// the year 1970 to 2038
//--------------------------
// countdown function
// parameters: (year, month, day, hour, minute)
if (empty($content)){$content = '';}
$content .= countdown(2008,12,14,12,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 " ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
//echo " ".$days_left." days ".$hours_left." hrs ".$minutes_left." mins";
$content = "Today's date ".date("F j, Y, g:i a")."<br/>";
$content .= " ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
$content .= " ".$days_left." days ".$hours_left." hrs ".$minutes_left." mins";
return $content;
}
?>
Palbin Site Admin Joined: Mar 30, 2006 Posts: 2404 Location: Pennsylvania
Posted:
Sun Nov 30, 2008 12:10 pm
Look up mktime() at the php site. It will tell you what all the variables are for.
Note: if you have php 5.1 or greater the last variable has become deprecated.
jakec Site Admin Joined: Feb 06, 2006 Posts: 3028 Location: United Kingdom
Posted:
Sun Nov 30, 2008 12:55 pm
Cheers Palbin.
CodyG Life Cycles Becoming CPU Cycles Joined: Jan 02, 2003 Posts: 666 Location: Vancouver Island
Posted:
Sun Nov 30, 2008 1:01 pm
Quote: Variables do not come out of functions for the most part unless you return them.
You rock! I've been trying to understand that concept in programming nuke for a long, long time. So many functions, so few returns.
Countdown block now works great. Except one little thing I'd like to add ... what happens after $the_countdown_date?
Palbin Site Admin Joined: Mar 30, 2006 Posts: 2404 Location: Pennsylvania
Posted:
Sun Nov 30, 2008 6:36 pm
What are you counting down? What would you like to happen?
I like to think the world will end!!
View next topic
View previous topic
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum