473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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("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
5 7868
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

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
2
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...
2
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 ...
13
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...
32
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...
3
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...
12
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...
11
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()...
3
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,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.