PDA

View Full Version : Custom function gone crazy.



AmenKa
09-16-2008, 04:59 AM
I am sure I have made a relatively stupid error, a helping hand would be great!

My code:


<?php
// Returns an acurate number of seconds in the current month.
function months_seconds() {
if(date(n) = 1) { // January.
$jan = date(n) * 31 * 24 * 60 * 60;
return($jan);
} elseif(date(n) = 2) { // February.
if(date(L) = 1) { // Leap year.
$lfeb = date(n) * 29 * 24 * 60 * 60;
return($lfeb);
} elseif(date(L) = 0) { // No leap year.
$nfeb = date(n) * 28 * 24 * 60 * 60;
return($nfeb);
}
} elseif(date(n) = 3) { // March.
$mar = date(n) * 31 * 24 * 60 * 60;
return($mar);
} elseif(date(n) = 4) { // April.
$apr = date(n) * 30 * 24 * 60 * 60;
return($apr);
} elseif(date(n) = 5) { // May.
$may = date(n) * 31 * 24 * 60 * 60;
return($may);
} elseif(date(n) = 6) { // June.
$jun = date(n) * 30 * 24 * 60 * 60;
return($jun);
} elseif(date(n) = 7) { // July.
$jul = date(n) * 31 * 24 * 60 * 60;
return($jul);
} elseif(date(n) = 8) { // August.
$aug = date(n) * 31 * 24 * 60 * 60;
return($aug);
} elseif(date(n) = 9) { // September.
$sep = date(n) * 30 * 24 * 60 * 60;
return($sep);
} elseif(date(n) = 10) { //October.
$oct = date(n) * 31 * 24 * 60 * 60;
return($oct);
} elseif(date(n) = 11) { // November.
$nov = date(n) * 30 * 24 * 60 * 60;
return($nov);
} elseif(date(n) = 12) { // December.
$dec = date(n) * 31 * 24 * 60 * 60;
return($dec);
}
}
echo(months_seconds());
?>


The error: "Fatal error: Can't use function return value in write context in /home/shadowte/public_html/months_seconds.php on line 4"

This simple function gets the month of the year, then converts that into seconds.

As I said, im sure I have made some stupid error in my attempt to simplify the process of getting the current month's number of seconds. Any help would be greatly appreciated.

Moshambi
09-16-2008, 05:34 AM
not sure if this will help you out on your error but you should probably change all of your "=" in your ifs and else ifs to "=="...

AmenKa
09-16-2008, 01:30 PM
We have a winner! Thank you! Funny how those stupid little things kill you when you aren't sharp.

Twey
09-16-2008, 03:13 PM
*puts a define('L', 2500) at the top of your script and watches the function break*

By default, PHP will convert undefined constants like L and n in that script to strings. However. you must never, ever rely upon this. Put them in quotes.

That's one redundant function. It can be better written so:
function seconds_in_month($time = false) {
if ($time === false)
$time = time();

list($y, $m) = explode('-', date('Y-m', $time));
return strtotime(sprintf('%s-%s-01', $y, $m + 1)) - strtotime(sprintf('%s-%s-01', $y, $m));
}

Moshambi
09-16-2008, 08:36 PM
We have a winner! Thank you! Funny how those stupid little things kill you when you aren't sharp.

Lol I wasn't sure if that was gonna fix your problem but I'm glad it did!

AmenKa
09-16-2008, 09:48 PM
*puts a define('L', 2500) at the top of your script and watches the function break*

By default, PHP will convert undefined constants like L and n in that script to strings. However. you must never, ever rely upon this. Put them in quotes.

That's one redundant function. It can be better written so:
function seconds_in_month($time = false) {
if ($time === false)
$time = time();

list($y, $m) = explode('-', date('Y-m', $time));
return strtotime(sprintf('%s-%s-01', $y, $m + 1)) - strtotime(sprintf('%s-%s-01', $y, $m));
}

I was actually wondering yesterday if that mattered, good to have it cleared up.