"include" different every day of the week

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

Post Reply
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

"include" different every day of the week

Post by pirolilla »

I need aid with script PHP. I want to open a different page in "include" depending on the day of the week, with this scheme:

Monday, Tuesday, Wednesday, Thursday and Friday = > page01.inc

Saturdays = > page02.inc

Sundays = > page03.inc

You can help me in this problem. I am not very expert in PHP.

Thanks.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You can get the day of the week with time() and date().

Next, set up a switch/case:

Code: Select all

<?php

switch($day_of_week)
{
    case 'Monday' or 'Tuesday' or [..etc]:
    include('path/to/file1.php');
    break;

    // and so on

?>
Last edited by McGruff on Tue Aug 09, 2005 10:01 pm, edited 1 time in total.
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

I am going to prove.

Thank you very much.

The express of the answer surprises to me.
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

I am not very expert in PHP and I am afraid that I need more aid. This it is the code that I have put in the page:

Code: Select all

<?php
switch($day_of_week)
{ 
case $day_of_week='Monday':
include('vidi01.inc');
break; 
case $day_of_week='Tuesday':
include('vidi01.inc');
break; 
case $day_of_week='Wednesday':
include('vidi01.inc');
break; 
case $day_of_week='Thursday':
include('vidi01.inc');
break; 
case $day_of_week='Friday':
include('vidi01.inc');
break; 
case $day_of_week='Saturday': 
include('visa01.inc');
break; 
case $day_of_week='Sunday': 
include('vido01.inc');
break; 
}
?>
But it does not work. Today (Saturday) opens include of Monday. That I am making bad?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Be careful with the difference between "=" and "==".

$var = 1 assigns a value to a var.
$var == 1 would be used to test if $var equals 1.

Try this:

Code: Select all

<?php
switch($day_of_week)
{ 
    case 'Monday' or 'Tuesday' or 'Wednesday' or 'Thursday' or 'Friday':
    include('vidi01.inc');
    break; 

    case 'Saturday': 
    include('visa01.inc');
    break; 

    case 'Sunday': 
    include('vido01.inc');
    break; 
}
?>
Last edited by McGruff on Tue Aug 09, 2005 10:01 pm, edited 1 time in total.
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

With that code it does not load any of the "includes". :(

Thank you very much anyway.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Ooops - this works:

Code: Select all

<?php
switch($day_of_week)
{
    case 'Monday':
    include('vidi01.inc');
    break;

    case 'Tuesday':
    include('vidi01.inc');
    break;

    case 'Wednesday':
    include('vidi01.inc');
    break;

    case 'Thursday':
    include('vidi01.inc');
    break;

    case 'Friday':
    include('vidi01.inc');
    break;

    case 'Saturday':
    include('visa01.inc');
    break;

    case 'Sunday':
    include('vido01.inc');
    break;
}
?>
Last edited by McGruff on Tue Aug 09, 2005 10:01 pm, edited 1 time in total.
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

I have completed script with this code and now works.

Code: Select all

<?php
$day = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') ;
$date = date(w) ;
$day_of_week = "$day[$date]" ;

switch($day_of_week) 
{ 
case 'Monday': 
include('vidi01.inc'); 
break; 

case 'Tuesday': 
include('vidi01.inc'); 
break; 

case 'Wednesday': 
include('vidi01.inc'); 
break; 

case 'Thursday': 
include('vidi01.inc'); 
break; 

case 'Friday': 
include('vidi01.inc'); 
break; 

case 'Saturday': 
include('visa01.inc'); 
break; 

case 'Sunday': 
include('vido01.inc'); 
break; 
}
?>
Thousands of thanks.

This forum is fantastic, I must a beer to you.

:lol: :lol: :lol: :lol: :lol:
pirolilla
Forum Newbie
Posts: 13
Joined: Sat Jan 24, 2004 1:24 am

Post by pirolilla »

Also is valid next code, simpler.

Code: Select all

<?php
$day_of_week = date(w) ;

switch($day_of_week){
    case 0:
        include('vido01.inc');
        break;
    case 6:
        include('visa01.inc');
        break;
    default:
        include('vidi01.inc');
        break;
}
?>
Thanks again.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yes - much better.
Post Reply