HELP! How to Get Date this Sunday and this Monday

This is a discussion on HELP! How to Get Date this Sunday and this Monday within the PHP Language forums, part of the PHP Programming Forums category; Hello, Today is Thursday, August 18, 2004. I would like to have a variable that stores the date this coming ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-19-2004
Eric Linders
 
Posts: n/a
Default HELP! How to Get Date this Sunday and this Monday

Hello,

Today is Thursday, August 18, 2004. I would like to have a variable
that stores the date this coming Sunday (even if today happened to be
Sunday) in YYY-MM-DD format. I also need a variable that will store
the date this past Monday (even if today happens to be Monday) also in
YYY-MM-DD format.

So, the end result is that, for this week (today is 2004-08-18) I
would have the following two variables:

$this_sunday = 2004-08-21 // This coming Sunday
$this_monday = 2004-08-14 // This past Monday

Thank you very much in advance for any help!

- Eric
Reply With Quote
  #2 (permalink)  
Old 08-19-2004
Doug
 
Posts: n/a
Default Re: HELP! How to Get Date this Sunday and this Monday


Well, you'll probably have to ask a girl out for each day.

-d

Reply With Quote
  #3 (permalink)  
Old 08-19-2004
Steven C. Gallafent
 
Posts: n/a
Default Re: HELP! How to Get Date this Sunday and this Monday

"Eric Linders" <elinders88@hotmail.com> wrote in message
news:491326e5.0408181959.717b130d@posting.google.c om...
> Today is Thursday, August 18, 2004. I would like to have a variable
> that stores the date this coming Sunday (even if today happened to be
> Sunday) in YYY-MM-DD format. I also need a variable that will store
> the date this past Monday (even if today happens to be Monday) also in
> YYY-MM-DD format.
>
> So, the end result is that, for this week (today is 2004-08-18) I
> would have the following two variables:
>
> $this_sunday = 2004-08-21 // This coming Sunday
> $this_monday = 2004-08-14 // This past Monday


Actually, this Sunday is the 22nd and Monday was the 16th, so you're going
to have a hard time getting a well-written function to return those values.
:)

<?PHP

$today = unixtojd();

$this_sunday = $today + (7 - jddayofweek($today)) % 7;
$last_monday = $today - (jddayofweek($today) + 6) % 7;

print "Today is " . jdtogregorian($today) . ".\n";
print "This Sunday is " . jdtogregorian($this_sunday) . ".\n";
print "Last Monday is " . jdtogregorian($last_monday) . ".\n";

?>

I'll leave it to you to convert these to yyyy-mm-dd.

Steve
--
Steven C. Gallafent - The Computer Guy, Inc.
steve@compguy.com - http://www.compguy.com/


Reply With Quote
  #4 (permalink)  
Old 08-19-2004
Gordon Burditt
 
Posts: n/a
Default Re: HELP! How to Get Date this Sunday and this Monday

>Today is Thursday, August 18, 2004. I would like to have a variable

In my world, August 18, 2004 is a WEDNESDAY.

>that stores the date this coming Sunday (even if today happened to be
>Sunday) in YYY-MM-DD format.


This format has a severe Y1K problem.

>I also need a variable that will store
>the date this past Monday (even if today happens to be Monday) also in
>YYY-MM-DD format.


This format still has a severe Y1K problem.

>So, the end result is that, for this week (today is 2004-08-18) I
>would have the following two variables:
>
>$this_sunday = 2004-08-21 // This coming Sunday

Um, the 21st is a SATURDAY.
>$this_monday = 2004-08-14 // This past Monday

Um, the 14th is a SATURDAY.

date('w') is the current weekday (0-6).

$x = (0 - date('w')) % 7;
is the number of days you need to add to the current date to
get to next Sunday. (Assuming that if today IS Sunday, you
want today). 0=Sunday If today is Thursday, that's (0-4)%7,
which is 3 days to add.

$x = (date('w') - 1) % 7;
is the number of days you need to subtract from the current
date to get to last Monday. (Assuming that if today IS Monday,
you want today.) 1=Monday If today is Thursday, that's (4-1)%7,
which is 3 days to subtract.

date('Y-m-d', time() + $x*24*60*60)
is the date $x days before (if $x is negative) or after
today in the format you want above. WARNING: there will
be glitches in this around daylight savings time transitions.
I think date('Y-m-d', mktime(0, 0, 0, date('m)', date('d')+$x,
date('Y'))) works better for this (UNIX mktime() allows you
to feed it things like March -1, 2004 and end up with
2004-02-29. I'm not sure that there can't be glitches in
this due to leap seconds.

Gordon L. Burditt
Reply With Quote
  #5 (permalink)  
Old 08-19-2004
Justin Wyer
 
Posts: n/a
Default Re: HELP! How to Get Date this Sunday and this Monday

Eric Linders wrote:

> Hello,
>
> Today is Thursday, August 18, 2004. I would like to have a variable
> that stores the date this coming Sunday (even if today happened to be
> Sunday) in YYY-MM-DD format. I also need a variable that will store
> the date this past Monday (even if today happens to be Monday) also in
> YYY-MM-DD format.
>
> So, the end result is that, for this week (today is 2004-08-18) I
> would have the following two variables:
>
> $this_sunday = 2004-08-21 // This coming Sunday
> $this_monday = 2004-08-14 // This past Monday
>
> Thank you very much in advance for any help!
>
> - Eric


Ok now after u read the above 3 posts here is the PROPER way to do it,
people really should read the php manual.

if (date("l") != "Sunday") // Is today Sunday?
$this_sunday = date("Y-m-d", strtotime("next Sunday")); // No
else
$this_sunday = date("Y-m-d"); // Yes
if (date("l") != "Monday") // Is today Monday?
$this_monday = date("Y-m-d", strtotime("last Monday")); // No
else
$this_monday = date("Y-m-d"); // Yes

And there you have it.

Although the dates you get from this won't match the dates in your
world, you seem to live in a very different timezone.

Reply With Quote
  #6 (permalink)  
Old 08-19-2004
Steven C. Gallafent
 
Posts: n/a
Default Re: HELP! How to Get Date this Sunday and this Monday

"Justin Wyer" <justin@isogo.co.za> wrote in message
news:cg1u10$eue$1@ctb-nnrp2.saix.net...
> Ok now after u read the above 3 posts here is the PROPER way to do it,
> people really should read the php manual.


I suggest that should be corrected to "here is A proper way to do it." There
is more than one way to skin this cat. Any of the approaches suggested will
work just fine. Pick your favorite.

Don't forget to read the docs for strtotime so that you are aware of
differences in how older versions of PHP handle this.

Steve
--
Steven C. Gallafent - The Computer Guy, Inc.
steve@compguy.com - http://www.compguy.com/


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0