Jump to content

problem with date()


Recommended Posts

Can you tell me what's wrong?It was working for few days and now my freind just updated some picture on the site and for some funny reason it stop working.

<?php	// beginning of switch_theme	$theme_switch_hour = date(G); // look at the local time from 0 to 23	// if it's between 7 am and 8 pm show day else show night, if unknow show unknow	if ($theme_switch_hour>=7 && $theme_switch_hour<20) {		echo "this is the day";	} else if (($theme_switch_hour>=0 && $theme_switch_hour<7) or ($theme_switch_hour>=20 && $theme_switch_hour<23)) {		echo "this is the night";	} else {		echo "I don't know!";	}	// ending of switch_theme?>

But after he toke off the

} else if (($theme_switch_hour>=0 && $theme_switch_hour<7) or ($theme_switch_hour>=20 && $theme_switch_hour<23)) {		echo "this is the night";

to see if it still work (he think that part might be the problem) and now it work fine.Can someone can explain what happened?Why was it working until that time?Thank you for clearing this up for meMatpatnik

Link to post
Share on other sites

i dont know actually, but maybe its becouse else if chould use elseif and you chould also remove or and set || insted, and you chould maybe remove the two (( at the start and have one. like

<?php	// beginning of switch_theme	$theme_switch_hour = date(G); // look at the local time from 0 to 23	// if it's between 7 am and 8 pm show day else show night, if unknow show unknow	if ($theme_switch_hour>=7 AND $theme_switch_hour<20) {		echo "this is the day";	} elseif ($theme_switch_hour >= 0 AND $theme_switch_hour < 7 AND $theme_switch_hour >= 20 AND $theme_switch_hour < 23){		echo "this is the night";	} else {		echo "I don't know!";	}	// ending of switch_theme?>

but like i said i really dont know :)-> Kristian_C

Link to post
Share on other sites
else if (($theme_switch_hour>=0 && $theme_switch_hour<7) or ($theme_switch_hour>=20 && $theme_switch_hour<23))

Shouldn't "or" be "||" like the following?
else if (($theme_switch_hour>=0 && $theme_switch_hour<7) || ($theme_switch_hour>=20 && $theme_switch_hour<23))

EDIT: Sorry Kristian_C, I glazed over your response and didn't see that you said the same thing. :)

Link to post
Share on other sites

The operators are fine, you can use OR or ||, and AND or &&, the only difference is precendence.http://www.php.net/manual/en/language.oper...tors.precedenceThe only thing conceptually wrong with the code is that you leave out 23. It should be <=23, not <23. But, you don't even need the second if statement. If the first IF succeeds, then it is day, or else it's night, you don't need to check again. If it's not day, then it has to be night. So just make it this:

<?php	// beginning of switch_theme	$theme_switch_hour = date(G); // look at the local time from 0 to 23	// if it's between 7 am and 8 pm show day else show night, if unknow show unknow	if ($theme_switch_hour>=7 && $theme_switch_hour<20) {		echo "this is the day";	} else {		echo "this is the night";	}	// ending of switch_theme?>

It would never not "know" what time it is. But, chances are, he updated something at 11pm and it just so happened that during that hour it wasn't able to figure it out because the logic was wrong.

Link to post
Share on other sites

Thank you guys, I have an other question related to the date()How can I use the date() function so it check the hour of the browser insted of the hour of the server so the costumer will see the theme_switch without having to fill a form?

Link to post
Share on other sites

The server does not have access to the client's time zone. You can use Javascript to check the client's time, but the server would not have access to anything you do with Javascript either unless you use AJAX or redirect to another page with the time zone specified.

Link to post
Share on other sites

here is how I would do it:java script:

Date = new Date();Hours = Date.getHours();document.getElementById('formid').hiddeninputelement.value=Hours;document.getElementById('formid').submit();

then of course you need a form:

<form id="formid" action="dayornight.php" method="post"><input type="hidden" name="hiddeninputelement" value="" /></form>

and then on the PHP page you check if $_POST['hiddeninputelement'] is set then if it isn't, redirect to the JS page. if it is, set $theme_switch_hour to $_POST['hiddeninputelement'].

Link to post
Share on other sites

Sorry for the missunderstanding, I didn't meant the actual date.I send the values thruogh a form, I just dont know how would a normal date variiable would work if i write only 02-06 for example(02-month, 06-day) or 02/06 or 02.06 without defining the year.. And when it comes to query I would like to use simply somthing like this: select * from table where between month(12) and month(2)Is this correct? Would this give me the winter months? How should I do it? I would like to add records with date variables but without the year defined. Could somebody help me? Thanks for the fast reply though

Link to post
Share on other sites

You can use 1 colon for the month and 1 for the day from your database tableand assign the right variable to each colons

SELECT $month_colon FROM $yourtable WHERE $month_colon <= 2 AND $month_colon = 12

I didn't test it but I think you are looking for something like that :)* I'm not sure if you can write <= side by side in the database query

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...