By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

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

P: n/a
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("28 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
Share this Question
Share on Google+
5 Replies


P: n/a
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("28 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($date,$day){
return date('d M Y',strtotime($day, strtotime($date)));
}

echo 'First Monday after 28 July 2005 is ';
echo weekdayAfter("28 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

P: n/a

"Emmanuel Ayrault" <em**************@nospamvoila.fr> schreef in bericht
news:42*********************@news.club-internet.fr...
return date('d M Y',strtotime($day, 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 firstdisplaytime and lastdisplaytime and I
made a webform where the staff can update the scheduling. Up to now
firstdisplaytime 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

P: n/a
Martien van Wanrooij a écrit :
So I decided to create two fields firstdisplaytime and lastdisplaytime and I
made a webform where the staff can update the scheduling. Up to now
firstdisplaytime 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("last Monday")).'<br />';
echo date('Y-m-d',strtotime("Monday")).'<br />';
echo date('Y-m-d',strtotime("next 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

P: n/a

"Emmanuel Ayrault" <em**************@nospamvoila.fr> schreef in bericht
news:42*********************@news.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

P: n/a
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 "celebration" 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,$user,$pass);
mysql_select_db($table,$connect);

$query="SELECT begin_date,end_date FROM date_conf";
$result=mysql_query($query,$connect);

$row=mysql_fetch_array($result);

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

$date_end=($row['end_date']!='')? $row['end_date'] :
date('Y-m-d',strtotime("Monday"));
$requete="SELECT * 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 discussion thread is closed

Replies have been disabled for this discussion.