Check day of 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
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Check day of week

Post by nite4000 »

hey everyone i am trying to check the day of week to see if a task can be performed. here is what i am trying to do.

1. Check to see if a setting is equal to 1 in the database.
2. if it is 1 then the cron file that runs once a day would have code to chk the value of the settings is 1 and then from there would chk the day of week.
3. if the day of the week is MON thru FRI then it would go on to do the cron job code already in place. if the day is saturday or sunday then it would NOT do the cron job. I hope i am making since

here is my code

Code: Select all

<?php
session_start();
require_once('inc/db.php');
$dbc = db_connect();



  $q = mysql_query("SELECT * FROM package_settings WHERE id='1'") or die(mysql_error());
  $pack_settings = mysql_fetch_array($q, MYSQL_ASSOC);
  @mysql_free_result($q);

if($pack_settings['week_only_pay']='1')
{
//$day_of_week = date("l");//get todays weekday
$day_of_week = 'Saturday'; //test to see if it would work but will be removed

if($day_of_week != 'Saturday' || 'Sunday')//check to be sure today isnt a weekend
{
	echo'You get paid';
}
else
{
	echo 'Sorry its a weekend';
}



} else 
{


//do cron work if today is anyday but sat or sun
echo'today isnt saturday or sunday so we can do our normal thing here';
	
}

?>

if anyone can fix the code or tell me what i may be missing it would help.


Thanks
zyntrax
Forum Commoner
Posts: 32
Joined: Wed Apr 13, 2011 2:23 am
Location: Sweden

Re: Check day of week

Post by zyntrax »

Hi there is a function for it:
date("l",mktime());
Shows full name of the day
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: Check day of week

Post by nite4000 »

yeah that i have but i am trying to chk the db for the value which i know how to do but i need to have it chk the day then depending on the result have it do either the cron code or not do it.
zyntrax
Forum Commoner
Posts: 32
Joined: Wed Apr 13, 2011 2:23 am
Location: Sweden

Re: Check day of week

Post by zyntrax »

Well depending on how your table looks, but i think it should work.

Code: Select all

<?php
session_start();
require_once('inc/db.php');
$dbc = db_connect();
$q = mysql_query("SELECT * FROM package_settings WHERE id='1'") or die(mysql_error());
$pack_settings = mysql_fetch_array($q, MYSQL_ASSOC);
@mysql_free_result($q);
$pay = $pack_settings[week_only_pay];
if($pay == 1)
{
	echo 'F off! come back on monday';
} 
else
{
	//do cron work if today is anyday but sat or sun
	echo'today isnt saturday or sunday so we can do our normal thing here';   
}

?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Check day of week

Post by McInfo »

nite4000 wrote:

Code: Select all

if($day_of_week != 'Saturday' || 'Sunday')
Comparison operators, like !=, are binary operators. That means they compare two values. In the expression $day_of_week != 'Saturday', the "not equal" operator compares the variable value on its left to the string value on its right and the expression is thus folded into a single boolean value.

Logical operators, like ||, also are binary operators which compare left and right values. Consider the expression 'Saturday' || 'Sunday'. Because the string "Saturday" is equivalent to boolean true, that particular expression is always true. (An "or" expression is short-circuited when a truth value is encountered on the left side. Likewise, an "and" expression is short-circuited when a falsehood is encountered on the left side.)

The "not equal" operator has a higher precedence than the "or" operator, so it is evaluated first. In the expression $day_of_week != 'Saturday' || 'Sunday', the "not equal" expression is folded into some boolean value first, so the expression becomes either true || 'Sunday' or false || 'Sunday'. The string "Sunday" is always true, so the expression then becomes either true || true or false || true. Both expressions are always true.
Post Reply