How to get first weekday in the month...

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
newtophp1
Forum Newbie
Posts: 2
Joined: Mon Apr 03, 2006 6:07 pm

How to get first weekday in the month...

Post by newtophp1 »

Hi,

I would like to insert 1 or 0 in my mysql Table depending on "first weekday in a month" (eg: Apr 3rd, 2006 or May 1st, 2006 or July 3rd, 2006 etc).

I can insert 0 or 1 if 1st of the month comes on Mon - Friday..but I could not insert 1 0r 1 if it falls on Sun/Sat'day eg: Apr 3rd, 2006 or jul 3rd, 2006...

Could you please help me achedive this in PHP/mysql script ...

Thanks in advance...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I don't have a clue what you're trying to do... But with http://www.php.net/strotime, http://www.php.net/getdate and http://www.php.net/mktime you almost get what you need...


Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

$day = 1;
$month = 4;
$year = 2006;

$date = getdate(mktime(0, 0, 0, $month, $day, $year));
echo sprintf('weekday of %d-%d-%d is %d', $year, $month, $day, $date['wday']);
?>
newtophp1
Forum Newbie
Posts: 2
Joined: Mon Apr 03, 2006 6:07 pm

Post by newtophp1 »

Hi TimVW, it does not give correct result. It shows.. "weekday of 2006-4-1 is 6" but the weekday in April is 3rd.


here is my problem:

1. I have a mysql table called Table1. It has 'StartOfMonth' as one of column.
2. I would like to insert '1' on the first weekday of the month (eg: on Apr 3rd, May 1st, June 1st, July 3rd etc)

How do I acheive this in PHP/mysql script...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

How hard is it with the code that i posted?

A simple if else construct for the following two situations:

If the wday is 1 to 5 you're done (mon - fri)
If the wday is 6 or 7 you have to add 1 or 2 days to get at mon.
Post Reply