?
Solved

Need function to find previous thursday OR friday

Posted on 2008-11-03
5
Medium Priority
?
324 Views
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 :-)
0
Comment
Question by:trg_dk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
lpxtech earned 2000 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

0
 
LVL 6

Expert Comment

by:lpxtech
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
0
 
LVL 16

Expert Comment

by:alain34
ID: 22865500
Hello trg_dk,

You should look at the strtotime function http://uk3.php.net/manual/en/function.strtotime.php

For example

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


Regards,

alain34
0
 
LVL 2

Author Comment

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

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

Thanks again,
Mark
0
 
LVL 6

Expert Comment

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

Glad I could help.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

764 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