Need function to find previous thursday OR friday

Posted on 2008-11-03
Last Modified: 2010-05-18
Hi :-)
I need a function that will return the date for the previus Thursday OR Friday (decided from a $parameter).

The date returned has to be at least 8 days before today - I will be calling this function twice to get the last full week going from friday -> thursday next week.

So today I would need to find 2 dates - Friday 10/24 and Thursday 10/30 - then use them later to perform a query.

I was building this function, where I owuld send 5 for thursday and 6 for friday - but it don't really work ::

function getStartDates($day=6){
      $dow = date("w"); //monday == 1
      $offset = ($dow -1);
      if ($offset <0) {
            $offset = $day;
      return date("Ymd", mktime(0,0,0,date('m'), date('d')-$offset-1, date('Y') ));

Hope that some of you guys can help me out here :-)
Question by:trg_dk
  • 3

Accepted Solution

lpxtech earned 500 total points
ID: 22865481
Here ya go. php built in function strtotime();
<?php echo date('m/d/Y', strtotime("last Thusday")); ?>

<?php echo date('m/d/Y', strtotime("last Friday")); ?>

Open in new window


Expert Comment

ID: 22865485
By the way, the strtotime() function returns a unix timestamp which is why I wrapped it in a date() tag The above code will echo 10/30/2008 and 10/31/2008
LVL 16

Expert Comment

ID: 22865500
Hello trg_dk,

You should look at the strtotime function

For example

$dateLastThursday = date("Y-m-d",strtotime("last Thursday"));



Author Comment

ID: 22865521
:-O OK this just blows my head clean off :-)

Thank you so much - feel I should have found this on ??
Are these function undocumented or did I fail miserably in the "Googling" categoru ?

Thanks again,

Expert Comment

ID: 22865575
The php manual should be your best friend. :-) If all else fails, check

Glad I could help.

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

Title # Comments Views Activity
can windows 10 wamp send mail()? 3 32
mysqli 3 18
datetime in sql 6 22
Bootstrap collapse causes odd behavior with php loop 7 20
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

930 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

8 Experts available now in Live!

Get 1:1 Help Now