Page 1 of 1

parameter list and their problems

Posted: Fri Jul 26, 2002 8:39 am
by somegone
i wanted to write an alias for the printf function which automaticly puts a br and a newline on the end of the line.
either it has to work like this:
<code>
function my_printf () {
printf (
do_something_important(func_get_args())
);
echo "<br>\n";
}

// so the line:
my_printf ("Hello World. Today it's the %d.!", (int)date("d"));
// will have the output line:
// Hello World. Today it's the 25.!<br>\n

</code>

but i have no idea how to convert the argument-array to use it again as parameter-list for the printf function.
so the do_something_important function has to split the array in the single arguments and put it in the right format ...
i know that this would work in C because i've tried it. but i wonder how it works in php

or it hast to work with a kind of makro ... i look forward to any kind of solution

second question:
is there any algorythm to calculate the week number out of a date?

thanks and sorry for my bad english =)
bye

Posted: Fri Jul 26, 2002 9:30 pm
by volka
you might want to take a look at func_num_args, func_get_arg and especially func_get_args

and for the second question getdate ;)

Posted: Sat Jul 27, 2002 4:15 am
by gnu2php
He already knows how to get the arguments, but the problem is passing those arguments to printf().

Here's a neat trick. Use eval(), like this:

Code: Select all

<?php

function my_printf()
&#123;
	$args = func_get_args();
	$num_args = count($args);

	$arg_list = '';
	for ($x = 0; $x < $num_args; $x++)
	&#123;
		if ($x) $arg_list .= ', ';
		$arg_list .= "\$args&#1111;$x]";
	&#125;

	eval("printf($arg_list);");

	echo "<br>\n";
&#125;

my_printf ("Hello World. Today it's the %d.!", date("w") + 1);

?>
All right! Post #100

Posted: Sat Jul 27, 2002 6:59 am
by volka
oops, I'm sorry. Haven't read the post to the end (skipped the code-section) :oops:

Posted: Mon Jul 29, 2002 1:00 am
by somegone
ok thanks about the parameter list problem
but the date("W") is not the right solution for my problem because it returns the week number of the current week.
but what if i want to know the week number of the 15th of august in 1984?

so i need a function which gets a date as parameter and returns the week number

for an example:
$week_nr = date_week_nr ("1984-08-15");

week_nr will have the week number of the week where the 15th of august was in 1984 ...

hope you know what i mean :)

Posted: Mon Jul 29, 2002 1:38 am
by twigletmac
Why don't you have a look at PHP's date and time functions:
http://www.php.net/manual/en/ref.datetime.php
and perhaps read about date() so that you can make it do what you want:
http://www.php.net/manual/en/function.date.php

Mac

Posted: Mon Jul 29, 2002 2:12 am
by gnu2php
Any date can be used if you pass a "timestamp" to the date() function:

Code: Select all

date("w", $timestamp)
If you have a date in the form "1984-08-15", use strtotime() to convert the string to a timestamp.

Code: Select all

date("w", strtotime("1984-08-15"))
(Note: A capitol 'W' won't work. It means "week number of year." Use a small case 'w')