Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Converting/Creating Blocks
Author Message
CodyG
Life Cycles Becoming CPU Cycles



Joined: Jan 02, 2003
Posts: 714
Location: Vancouver Island

PostPosted: Sat Nov 29, 2008 6:43 pm Reply with quote

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";
}

_________________
"We want to see if life is ubiquitous." D.Goldin 
View user's profile Send private message
jakec
Site Admin



Joined: Feb 06, 2006
Posts: 3048
Location: United Kingdom

PostPosted: Sun Nov 30, 2008 7:57 am Reply with quote

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 />";
 
View user's profile Send private message
jakec







PostPosted: Sun Nov 30, 2008 7:58 am Reply with quote

Oh yeah obviously paste the code into the Sample Block.
 
CodyG







PostPosted: Sun Nov 30, 2008 8:46 am Reply with quote

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: www.elouai.com
//
// 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";

?>

Sad
( 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 ....
Image


Last edited by CodyG on Sun Nov 30, 2008 9:48 am; edited 1 time in total 
jakec







PostPosted: Sun Nov 30, 2008 8:52 am Reply with quote

Hmm, your image doesn't appear to be displaying.
 
CodyG







PostPosted: Sun Nov 30, 2008 9:49 am Reply with quote

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: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Sun Nov 30, 2008 12:02 pm Reply with quote

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: www.elouai.com
//
// 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;
}

?>
 
View user's profile Send private message
Palbin







PostPosted: Sun Nov 30, 2008 12:10 pm Reply with quote

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







PostPosted: Sun Nov 30, 2008 12:55 pm Reply with quote

Cheers Palbin. Smile
 
CodyG







PostPosted: Sun Nov 30, 2008 1:01 pm Reply with quote

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. Wink

Countdown block now works great. Except one little thing I'd like to add ... what happens after $the_countdown_date?
 
Palbin







PostPosted: Sun Nov 30, 2008 6:36 pm Reply with quote

What are you counting down? What would you like to happen?

I like to think the world will end!! ROTFL
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Converting/Creating Blocks

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
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©