Jump to content


Date help

  • Please log in to reply
2 replies to this topic

#1 ruddernz

  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 02:36 AM

Hi there, im a bit stuck with a date problem. I have a form where the user types in a date and need to find records from the last friday of the previous month to the date typed in. Can get the 1st day of the previous month but have been trying to get the last friday of that month but bit stuck :s


#2 AV1611

  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 24 March 2006 - 12:15 PM

Can I suggest a solution that may not be the easy way, because I don't know if there is an easy way?

Create a loop that works like this:

create a variable that is the current month.
subtract 1 from that month.
create a variable that is the last day of that month
echo every day/date in a loop that counts backward until the day==friday


#3 kenrbnsn

  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 March 2006 - 01:06 AM

Here is a function I finally came up with ....
function last_friday($dt='today') {
    $prev = date('Y-m-d',strtotime('-1 month',strtotime($dt)));
    $lday = date('t',strtotime($prev));
    $prevm = date('m',strtotime($prev));
    $prevy = date('Y',strtotime($prev));
    $ld = date('Y-m-d',strtotime($prevm . '/' . $lday . '/' . $prevy));
    if (date('l',strtotime($ld)) == 'Friday') return $ld;
    else return date('Y-m-d',strtotime('last friday',strtotime($ld)));


(Why this originally posted twice)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users