methuselah
methuselah

Reputation: 13206

PHP date range problems

Today I am having a rather strange problem with my date range.

$oBeginDate = newDateTime('last wednesday');
$oEndDate = newDateTime('next tuesday');
echo $oBeginDate->format('d/m/Y') . ' to ' . $oEndDate->format('d/m/Y');

This should usually display 14/12/2010 to 21/12/2010, but today, 14/12/2010, it is displaying 08/12/2010 to 21/12/2010.

Any suggestions as to how I can fix this problem?

I would like the date to update from 08/12/2010 to 14/12/2010 automatically to keep the one week range.

Upvotes: 0

Views: 229

Answers (3)

Thomas Joulin
Thomas Joulin

Reputation: 6650

Today, December 14th, is a Tuesday. So last Wednesday is indeed December 8th.

Upvotes: 1

Mark Grey
Mark Grey

Reputation: 10257

Using the literals 'last wednesday' or 'next tuesday' returns a date relative to the current date time.

If you are attempting to get the range from today, the default parameter to the DateTime constructor is 'now' and will return the current date time.

Additionally a 'fixed' date in the future or the past would have to be declared explicitly.

If you are trying to get this week's date for that day, just pass the name explicitly.

    $startd = new DateTime('wednesday'); 

   $end_date = date_add($startd,date_interval_create_from_date_string('1 week'));
   echo $startd->format('d/m/Y').' to '.$endd->format('d/m/Y');

To have the range update day to day without reference to day of week, use 'now' instead of 'wednesday'.

Upvotes: 2

Mchl
Mchl

Reputation: 62369

Last Wednesday was on 8th. Not sure why would you expect 14th which is Tuesday.

Upvotes: 1

Related Questions