PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 4:20 am
Code: Select all
<?
$months;
$months['January'] = 31;
$months['March'] = 31;
$months['April'] = 30;
$months['May'] = 31;
$months['June'] = 30;
$months['July'] = 31;
$months['August'] = 31;
$months['September'] = 30;
$months['October'] = 31;
$months['November'] = 30;
$months['December'] = 31;
$now = date("Y");
$yr = 2000;
for($i=0;$i<10;$i++) {
$yr = $yr + 4;
if($now==$yr) {
$months['February'] = 29; }
else {
$months['February'] = 28; }
}
echo $months['February'];
?>
I wrote that script just now. I'm trying to set the number of days on each month, and if it's a leap year, it'll set February to 29.
But with this, I always get 28, no matter what. I tried changing the
to
so I'd get 2003 but it still appears 28. Any ideas?
Thanks,
-Nay
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Sep 27, 2003 7:02 am
Code: Select all
function leap_year_add ($year)
{
if ($year/4 == (int)($year/4) &&
($year/100 != (int)($year/100) || $year/400 == (int)($year/400)))
return 1; /* leap year. */
else
return 0; /* not leap year. */
}
I've used this. Might give you some ideas.
Edit: Fixed the php tags...
Last edited by
JAM on Sat Sep 27, 2003 7:13 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 7:11 am
What does the (int) do anyway? O_o
-Nay
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Sep 27, 2003 7:13 am
Typecasting. Making sure that the $year is really a number, rather than a string...
Strings dont work well with math something =)
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 7:15 am
I'm still a little O_o about the script. Well, my slow-memory-on-scripts-that-I-don't-write-and-beyond-my-thinking. Mind explaining each line for me JAM? =)
-Nay
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Sep 27, 2003 7:35 am
Not each line, but as a general tip.
Using (int) you typecast the value to something without decimals (floats/doubles). Dividing the year using 4, 100 and 400 is an algorithm that works with calculating leap years.
Code: Select all
// example to show you the differences
echo (2000/400)." : ".(int)(2000/400)."<br>";
echo (2001/400)." : ".(int)(2001/400)."<br>";
echo (2002/400)." : ".(int)(2002/400)."<br>";
echo (2003/400)." : ".(int)(2003/400)."<br>";
/* Results:
5 : 5
5.0025 : 5
5.005 : 5
5.0075 : 5
*/
So, if the years divided above, is the same as the the ones (int)-typecasted, it's a leapyear.
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 7:51 am
Code: Select all
<?
$months;
$months['January'] = 31;
$months['March'] = 31;
$months['April'] = 30;
$months['May'] = 31;
$months['June'] = 30;
$months['July'] = 31;
$months['August'] = 31;
$months['September'] = 30;
$months['October'] = 31;
$months['November'] = 30;
$months['December'] = 31;
$year = date("Y");
if ($year/4 == (int)($year/4) &&
($year/100 != (int)($year/100) || $year/400 == (int)($year/400)))
$months['February'] = 29;
else
$months['February'] = 28;
echo $months['February'];
?>
YAY! Works now =D. Well, it also worked before, it's just now that I understand it better =D.
-Nay
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 8:08 am
One more thing, about arrays. How do I find out "the other value"?
Say:
Code: Select all
$phpdn = array("nay", "jam", "jason", "mac");
So If I was given "Mac" then how do I find out that she is number 3 in the array. Or If I was given 2, then how would I find out it is jason?
-Nay
ps: don't take the array litterly, I just wrote the random name that appeared in my head.
igoy
Forum Contributor
Posts: 203 Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:
Post
by igoy » Sat Sep 27, 2003 9:50 am
This solves your second question.... still cheking your first one...
sorry for going backward..
Code: Select all
<?
$phpdn = array("nay", "jam", "jason", "mac");
echo $phpdn[2];
?>
this will output "jason"
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 10:41 am
Hey, my first question was already answered by Jason.
The second question, I'm not sure how you interpreted it but It wasn't what I was looking for. It's something like a maths equation:
a + 9 = 10
so you can guess that a is 1. If you were given:
b = 10 - 9
So what I want to know how to do is. If the answer (Value) from the array was given, how would I find out the number?
am I confusing you?
say Jason was given. How to find out if he was in "2".
of IF 2 was given, how to find out that it is Jason - without outputting to the browser.
-Nay
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Sep 27, 2003 10:48 am
Code: Select all
<?php
$phpdn = array("nay", "jam", "jason", "mac");
echo $phpdn[2], "\n";
echo 'key of "mac" is: ', array_search ('mac', $phpdn);
?>see also:
http://php.net/array_search
of IF 2 was given, how to find out that it is Jason - without outputting to the browser.
what do you mean?
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sat Sep 27, 2003 10:51 am
Oh wait.........my bad. Sorry for the second, stupid question.
Thanks volka,
-Nay
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Sep 27, 2003 1:04 pm
Hey! I'm number one!
err...
Just wanted to mention count($array) that might be worth looking into also, if you are considering playing around with dynamic arrays that you dont know the lenght (keys) of.
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Sun Sep 28, 2003 1:10 am
The final working script:
Code: Select all
<?
// month's number of days and names array
$months = array();
$months[0] = "January";
$months['January'] = 31;
$months["February"];
$months[1] = "February";
$months['March'] = 31;
$months[2] = "March";
$months['April'] = 30;
$months[3] = "April";
$months['May'] = 31;
$months[4] = "May";
$months['June'] = 30;
$months[5] = "June";
$months['July'] = 31;
$months[6] = "July";
$months['August'] = 31;
$months[7] = "August";
$months['September'] = 30;
$months[8] = "September";
$months['October'] = 31;
$months[9] = "October";
$months['November'] = 30;
$months[10] = "November";
$months['December'] = 31;
$months[11] = "December";
// set february's number of days
$year = date("Y");
if ($year/4 == (int)($year/4) &&
($year/100 != (int)($year/100) || $year/400 == (int)($year/400))) {
$months['February'] = 29; }
else {
$months['February'] = 28; }
// get next month
$this_month = date("F");
$int = array_search($this_month, $months);
$next_month = $int + 1;
$next_month = $months[$next_month];
// get 5 days from today
$today = date("d");
$this_month_days = $months[$this_month];
$tdue = $today + 5;
if($tdue > $this_month_days) {
$next_month_due = $tdue - $this_month_days;
$today + $tdue;
}
else {
}
// state mysql query
$q = "SELECT * FROM customers WHERE exp_day < $tdue AND exp_month = $this_month OR exp_day < $next_month_due AND exp_month = $next_month ORDER BY f_name DESC";
echo $q;
?>
All thanks to Volka, JAM and my vast knowledge of completely stupid questions
.
-Nay
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Sep 28, 2003 1:40 am
don't get me wrong, I know it's fun to do it yourself. But isn't it much easier to let predefined functions do the work?
Code: Select all
<?php
$dateNow = getdate();
$tsNextMonth = mktime(12, 0, 0, $dateNow['mon']+1, 1, $dateNow['year']);
$tsFiveDaysFromNow = mktime(12, 0, 0, $dateNow['mon'], $dateNow['mday']+5, $dateNow['year']);
echo date('F j, Y', $tsNextMonth), "\n", date('F j, Y', $tsFiveDaysFromNow);
?>