Jump to content

[SOLVED] Showing information during certain time ranges


RickyF

Recommended Posts

Hey everyone,

 

It is possible for PHP to display something within a certain time range - using the server time, e.g. if the time was between 8am and 9pm, something is shown, if not then something else is shown (talking in semi php, heh).

 

If so could anyone provide some help or even better the code for it?

 

Looked all over the net, cant find anything about this.

 

Any help is much appreciated.

 

 

Thanks! 8)

Link to comment
Share on other sites

Hey everyone,

 

It is possible for PHP to display something within a certain time range - using the server time, e.g. if the time was between 8am and 9pm, something is shown, if not then something else is shown (talking in semi php, heh).

 

If so could anyone provide some help or even better the code for it?

 

Looked all over the net, cant find anything about this.

 

Any help is much appreciated.

 

 

Thanks! 8)

 

use Date function to take out the hour only i.e. date('h'); , assign that to a variable and if statement to do whatever you want

 

 

Link to comment
Share on other sites

I've never done this before but something like

<?php
$time = date ("G");

if ($time >= 8 && $time <= 21){
//code to be implemented here
}
else {
//code to be shown when not between 8am and 9pm
}
?> 

 

There's probably a better way of formatting the if statement as well..

Link to comment
Share on other sites

I've never done this before but something like

<?php
$time = date ("G");

if ($time >= 8 && $time <= 21){
//code to be implemented here
}
else {
//code to be shown when not between 8am and 9pm
}
?> 

 

There's probably a better way of formatting the if statement as well..

 

 

seems ok to me  :)

Link to comment
Share on other sites

Thanks for your replys! and thank you Dragen for providing a code!  8)

 

Do you know how you would add the ability, so that if the date is for example, christmas day... the code echos a certain thing?

 

e.g., if time is 8am - 9pm its on, if not  its off, and the date is 12/25 or new years day 01/01, regardless of the times its off.

 

Hope that makes sense

 

Im sure it can easily added to the script written by Dragen - i dont have the knowledge to do so my self yet.

 

Heres the code i have edited slightly:

 

<?php
$time = date ("G");

if ($time >= 8 && $time <= 21){
echo "Online";
}
else {
echo "Offline";
}
?>

 

Thanks for any further help!

Link to comment
Share on other sites

well the date ("G"); only reads the time in 24 hour format.

I guess this might work:

<?php
$date = date ("nd");
$time = date ("G");

//checks if time is between 8am and 9pm
if ($time >= 8 && $time <= 21){
//code to be implemented here
}
else {
//code to be shown when not between 8am and 9pm
}

//checks if christmas day
if ($date == 1225){
//code to be implemented if xmas
}
else {
//code to be implemented if not xmas
}
?>

not sure if I've got the $date == 1225 part quite right...

Link to comment
Share on other sites

Thanks for your replys! and thank you Dragen for providing a code!  8)

 

Do you know how you would add the ability, so that if the date is for example, christmas day... the code echos a certain thing?

........

 

 

it's christmas already? where are my presents? :D

Link to comment
Share on other sites

Thanks again Dragen, how ever could you make it so that...

 

If its not between 8am - 9pm or if its christmas, off is shown, otherwise on is shown?

 

Otherwise the code wouldnt work properly as required.

 

 

Thanks!

Link to comment
Share on other sites

<?php
$date = date ("nd");
$time = date ("G");

//checks that time is either earlier than 8am or later than 9pm, or xmas
if ($time < 8 || $time > 21 || $date == 1225){
//code to be implemented here
}
else {
//code to be shown between 8am and 9pm on days that aren't xmas
}
?>

 

Again, not sure if it'll work.

 

I guess you could also seperate the $date = date ("nd"); part to make it easier to read, by using

$date = date ("n,d");

and replacing

$date == 1225

with

$date == 12,25

simply seperating the month and the day.

please note that I haven't tried this and you should test it with todays date or something to check that it works..

Link to comment
Share on other sites

Thats exactly how i need it, however the date check doesnt seem to work, i tried:

 

<?php

$date = date ("nd");

if ($date == 0408) {

echo "it works";

}
else {

echo "it doesnt work";

}
?>

 

i tried the date as 0408 and 48, which i assume one of those is correct for todays date?

 

And it just kept showing it didnt work.

 

FYI, my server is in UK date format, DD/MM/YYYY, if that makes a difference.

 

Thanks!

Link to comment
Share on other sites

ah yeah..

try this instead.. I used the wrong month variable

<?php
$date = date ("md");
$time = date ("G");

//checks that time is either earlier than 8am or later than 9pm, or xmas
if ($time < 8 || $time > 21 || $date == 0408){
//code to be implemented here
}
else {
//code to be shown between 8am and 9pm on days that aren't xmas
}
?>

Link to comment
Share on other sites

I have changed the time to 11 so the script works atm,

 

I set the date as today and the new time to 11, which will work untill 11:59pm, how ever its showing online, when it should be showing offline?

 

<?php

 

$date = date ("md");

$time = date ("G");

 

if ($time < 8 || $time > 23 || $date == 0408) {

 

echo "Offline";

 

}

else {

 

echo "Online";

 

}

?>

Link to comment
Share on other sites

no. up until 12 it should show online.

<?php

$date = date ("md");
$time = date ("G");

if ($time < 8 || $time > 23 || $date == 0408) {

echo "Offline"; //this part is what is shown before 8am and after 11pm

}
else {

echo "Online"; //this is what is shown during the day (between 8am and 11pm)

}
?>

So until it reaches 12 in the morning it will display online. If you want it to be offline at 11, then you need to specify minutes I think, instead of just hours.

so it would be

<?php

$date = date ("md");
$time = date ("Gi");

if ($time < 800 || $time > 2300 || $date == 0408) {

echo "Offline"; //this part is what is shown before 8am and after 11pm

}
else {

echo "Online"; //this is what is shown during the day (between 8am and 11pm)

}
?>

The 'i' on the $time = date ("Gi"); detects the minutes.

Link to comment
Share on other sites

Ahh right thanks, will test the date thing tomorrow and let you know ;D - Hopefully it works

 

How do you do 12am, 000 i would guess?

 

And i could add other dates as you did with christmas yes? e.g. if ($time < 800 || $time > 2100 || $date == 1225 || $date == 1010) {

 

Can you make the dates uk format, by changing $date = date ("md"); to $date = date ("dm"); ?

 

Thanks for your help!

Link to comment
Share on other sites

12 am would be 0000.

To add other dates you would simply do as you've suggested

($time < 800 || $time > 2100 || $date == 1225 || $date == 1010) {

Make sure that you use the or symbols ||.

 

the $date = date ("md"); just means that it is month, then day. changing it to $date = date ("dm"); ? would simply mean it would be day then month.

look at this page for a list of all the date constants: http://www.wdvl.com/Authoring/Languages/PHP/Date_Time/

Link to comment
Share on other sites

Hi,

 

The script is as following,

 

<?php
$date = date ("dm");
$time = date ("Gi");
if ($time < 800 || $time > 2100 || $date == 0904) {
$status = "offline";
}
else {
$status = "online";
}
?>

 

I've put todays date, so it should be showing offline, however online is being showed?

Link to comment
Share on other sites

Nooo, lol confusing aint it.

 

if its between 8-9 it needs to show as on, but if it was for example xmas day, it should show as offline no matter what time it is.

 

8-9 on

outside 8-9 off

8-9 on xmas off

outside 8-9 on xmas off

 

 

If that makes sense?

Link to comment
Share on other sites

oh right, I get ya!

sorry I was just getting confused..

I think this should work:

<?php
$date = date ("dm");
$time = date ("Gi");
if ($date == '0905') { //if $date is equal to xmas
$status = "offline"; //this now changes offline
}
else {
if($time >= '800' || $time <= '2100') {
$status = "online"; //changed this to set the online status
}
else {
$status = "offline"; //this now changes offline
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.