Solved

PHP Get 1st Day of Week

Posted on 2013-12-31
2
381 Views
Last Modified: 2013-12-31
Hi,

I'm trying to deal with taking the current date and getting the beginning day/date of the week (Monday being day 1).  Then using the calendar date of the 1st day of the week, I'm adding 6 to get the last day of the week.  My issue is dealing with year boundaries, as in this week's range is 12/30/2013 - 1/5/2014.  That's the issue...when the week has a year change in the middle.  I'm not wanting to use week numbers to get week range as I don't want to deal with week 53.

So, I've come up with the below code, but something's missing and I'm hoping someone can help me by spotting the obvious.

Thanks so much =)
function firstDayOfWeek($curdate)
	{
		$day1 = DateTime::createFromFormat('Y-m-d', $curdate);
		$day1->setISODate((int)$day->format('O'), (int)$day1->format('W'), 1);
		return "1st Day: ".$day1->format('Y-m-d');
	}

Open in new window

0
Comment
Question by:btuskey
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39748485
what about this technique:
$monday = strtotime('last monday', strtotime('tomorrow'));

Open in new window

0
 

Author Closing Comment

by:btuskey
ID: 39748653
Hi Guy,

I'm marking this as the accepted answer because it works and addressed my question as it would be read.  Unfortunately, I didn't ask the questions correctly as it wasn't the issue of getting the first day of the week, but more the last day (Sunday) and the week range...however, I resolved it myself once I stepped back and found the glaring issue later in my dynamic dropdown with the week range.

I appreciate your quick response!

Best = )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now