0

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.

3
  • 1
    newDateTime is not a php library function. I think you mean new DateTime Commented Dec 14, 2010 at 22:34
  • stackoverflow.com/questions/4212622/… Commented Dec 14, 2010 at 22:37
  • 2
    Please don't call me a flower. Commented Dec 14, 2010 at 22:50

3 Answers 3

2

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'.

Sign up to request clarification or add additional context in comments.

1 Comment

the date needs to update automatically to keep the one week range
1

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

Comments

1

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

2 Comments

it needs to keeps a one week range
you ask for last wednesday. php answers you correctly. What is it exactly that you want exactly ? I don't see any way to get 14/12/2010 (a tuesday) when you ask for last wednesday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.