473,545 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looking for a date function : "First Monday after a certain date"

I would like to retrieve, let us say, the First Monday after a certain date,
so my (imaginary) function
could be something like
echo weekdayAfter("2 8 July 2005", "Monday") should return "1 August 2005",
(or a timestamp that could be formatted).
I have been thinking about creating a loop that increases the source date
$myDate with 24*60*60 until date("l", $myDate) = "Monday" but maybe I am
trying too complicated solutions and overseeing more easy ones.
Any suggestions?

Martien van Wanrooij
Jul 30 '05 #1
5 7896
Martien van Wanrooij a écrit :
I would like to retrieve, let us say, the First Monday after a certain date,
so my (imaginary) function
could be something like
echo weekdayAfter("2 8 July 2005", "Monday") should return "1 August 2005",
(or a timestamp that could be formatted).
I have been thinking about creating a loop that increases the source date
$myDate with 24*60*60 until date("l", $myDate) = "Monday" but maybe I am
trying too complicated solutions and overseeing more easy ones.
Any suggestions?

Martien van Wanrooij


Maybe, you should try this (I'm not sure)
Of course, it's necessary to test the input values of the function.

<?
function weekdayAfter($d ate,$day){
return date('d M Y',strtotime($d ay, strtotime($date )));
}

echo 'First Monday after 28 July 2005 is ';
echo weekdayAfter("2 8 July 2005", "Monday");

?>

It seems to work except if you are on Monday (in fact, it depends on
what you want to do). In this case, you have to use "next Monday" with
strtotime function.

Take a look at the strtotime function: http://www.php.net/function.strtotime

--
Manu
Jul 30 '05 #2

"Emmanuel Ayrault" <em************ **@nospamvoila. fr> schreef in bericht
news:42******** *************@n ews.club-internet.fr...
return date('d M Y',strtotime($d ay, strtotime($date ))); At a first glance this is a good suggestion, I didn't realise that strtotime
could have a second parameter, I will certainly try that(in fact, it depends on what you want to do).

I didn't give the complete explanation why I needed it because I thought my
message would be too large but maybe you can still give me some suggestions:
on the website of our local church, the celebrations for next weekend are
retrieved from a Mysql database. Since they are usually on Saturday and
Sunday, I decided to refresh them on Monday. However in some cases it can be
practical to show the celebrations for more then one week at once (e.g. in
the week before Eastern there are more masses on other weekdays, Monday
after Eastern logically belongs to the weekend before, Christmas can be
every day of the week etc.). Such exceptions occur about five times a year.
I also tried other "refresh dates" like Tuesday but there will always be
exceptions.
So I decided to create two fields firstdisplaytim e and lastdisplaytime and I
made a webform where the staff can update the scheduling. Up to now
firstdisplaytim e and lastdisplaytime are updated manually but I would like
to write such a code that both fields are calculated automatically (monday
before and after the celebrations) unless these dates are filled in
explicitely by the staff.
Additional suggestions will be welcome, thanks anyway.

Martien.
Jul 31 '05 #3
Martien van Wanrooij a écrit :
So I decided to create two fields firstdisplaytim e and lastdisplaytime and I
made a webform where the staff can update the scheduling. Up to now
firstdisplaytim e and lastdisplaytime are updated manually but I would like
to write such a code that both fields are calculated automatically (monday
before and after the celebrations) unless these dates are filled in
explicitely by the staff.
Additional suggestions will be welcome, thanks anyway.


I'm afraid I don't understand all what you want to do. Could you give
some exemple with real dates of celebration and the period you want to
display them.

Otherwise, you can use strtotime to calculate the monday some weeks
before or after the current date:

<?php

echo 'Today: '.date('Y-m-d').'<br />';
echo date('Y-m-d',strtotime("-1 week last Monday")).'<br />';
echo date('Y-m-d',strtotime("l ast Monday")).'<br />';
echo date('Y-m-d',strtotime("M onday")).'<br />';
echo date('Y-m-d',strtotime("n ext Monday")).'<br />';
echo date('Y-m-d',strtotime("+ 1 week next Monday")).'<br />';
?>
After that, you just have to use a correct query in mysql to display the
dates you want to show between 2 limits.

Hope it can be useful.

--
Manu
Jul 31 '05 #4

"Emmanuel Ayrault" <em************ **@nospamvoila. fr> schreef in bericht
news:42******** *************@n ews.club-internet.fr...
I'm afraid I don't understand all what you want to do. Could you give
some exemple with real dates of celebration and the period you want to
display them.

Okay... Sometimes I prefer to give a very basical answer to such questions,
but when I do so, I often get reactions like: "you are using too many words
for explaining something that could have been explained more easily" ....
but I will tell it my own way now.
Today it is Sunday. If you look at www.parochiebeekendonk.nl/viering.php
you will see the celebrations that have been done yesterday (Saturday) and
today (Sunday). Usually there are no more celebrations until next Saturday
so Monday is a good day to refresh the scheduling for next weekend.
But sometimes the Monday after Easterm should be shown and at Eastern I
would like to switch the re-schedulling date to Wednesday or so. Christmas
is still more complicated. It is 25th of December and it could be any day of
the week. Let us say Christmas is on a Wednesday.. since many people are on
holiday it would be good to show all the celebrations from the weekend
*before* Christmas until the weekend *after* New Years Day... but this
doesn't change the fact that generally, there is just one scheduling for the
next weekend that can be refreshed every Monday..
What I would like to create is an interface that returns the Monday before
and the Monday after unless the user indicates explicitely another value for
this monday-before and monday-after field
Martien
Jul 31 '05 #5
Martien van Wanrooij a écrit :
Okay... Sometimes I prefer to give a very basical answer to such questions,
but when I do so, I often get reactions like: "you are using too many words
for explaining something that could have been explained more easily" ....
but I will tell it my own way now.
Maybe, It's my fault. I don't speak english fluently.
Today it is Sunday. If you look at www.parochiebeekendonk.nl/viering.php
you will see the celebrations that have been done yesterday (Saturday) and
today (Sunday). Usually there are no more celebrations until next Saturday
so Monday is a good day to refresh the scheduling for next weekend.
But sometimes the Monday after Easterm should be shown and at Eastern I
would like to switch the re-schedulling date to Wednesday or so. Christmas
is still more complicated. It is 25th of December and it could be any day of
the week. Let us say Christmas is on a Wednesday.. since many people are on
holiday it would be good to show all the celebrations from the weekend
*before* Christmas until the weekend *after* New Years Day... but this
doesn't change the fact that generally, there is just one scheduling for the
next weekend that can be refreshed every Monday..
What I would like to create is an interface that returns the Monday before
and the Monday after unless the user indicates explicitely another value for
this monday-before and monday-after field
Martien


It's possible to store a begin/end date in a table (or in a flat file).
You just have to implement an interface to modify it.

You have to search if the values exists else you assign them the value
of the last monday and the next monday.

After that, build the request to obtain the celebrations between these 2
dates.

Note: You don't need to update the values of the "celebratio n" table,
juste implement an interface to add new values. So, it's possible to
store the celebrations for months in the future. They will just appear
at the right period.
For example:

<?php
$connect=mysql_ connect($bd,$us er,$pass);
mysql_select_db ($table,$connec t);

$query="SELECT begin_date,end_ date FROM date_conf";
$result=mysql_q uery($query,$co nnect);

$row=mysql_fetc h_array($result );

$date_begin=($r ow['begin_date']!='')? $row['begin_date'] :
date('Y-m-d',strtotime("l ast Monday"));

$date_end=($row['end_date']!='')? $row['end_date'] :
date('Y-m-d',strtotime("M onday"));
$requete="SELEC T * FROM celebration where date >= $date_begin AND date
<= $date_end";
......
?>
This is not the best code for that, but it's a way to do that I think.
It depends on the build of your mysql tables

--
Manu

Jul 31 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2181
by: Westcoast Sheri | last post by:
When I used the "date("nj")" function to generate an invoice, about 1 out of 500 times, nothing comes up <?php $invoicedate = date("nj"); ?> <html><head></head><body> Invoice date: <?php print $invoicedate;
2
1421
by: bissatch | last post by:
Hi, I am trying to use the following function: echo date("M", $i) to echo the date that the value of a variable, $i, represents (eg. 1 = Jan, 5 = May, 11 = Nov). Unfortunetely, it returns only Jan for any
2
23338
by: ITM | last post by:
Does anyone have an example of an SQL query which returns rows for the year-to-date, but where the "year" commences on August 1st? e.g. select * from mytable where datefield > last august 1st TIA for any help Isabel
13
3067
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null...
32
4106
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:"...
3
11713
by: Reney | last post by:
I am using Access Database in my program. The column in the table that I am going to use has date/time value with Medium Time selected. (HH:mm). The program is recording a clock in time to this field, which is the time when the entry is made. If you check the database, it shows the correct time in the correct format. But when you are calling...
12
13848
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL does not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
11
21058
by: walterbyrd | last post by:
My MySQL table has a field that is set as type "date." I need to get today's date, and insert it into that field. The default for that MySQL field is 2006-00-00. I know about the date() function. I have tried date("Y-m-d") and date("U"), neither worked. The date field was just filed with all zeros. Can anybody point me in the right...
3
1553
by: davidfinance | last post by:
Ok, maybe this is a stupid question, but why can't I make a subclass of datetime.date and override the __init__ method? --- from datetime import date class A(date): def __init__(self, a, b, c, d): print a, b, c, d
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5341
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.