Jump to content

Archived

This topic is now archived and is closed to further replies.

ruddernz

Date help

Recommended Posts

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

thx

Share this post


Link to post
Share on other sites
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

;-P

Share this post


Link to post
Share on other sites
Here is a function I finally came up with ....
[code]<?php
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)));
}?>[/code]

Ken

(Why this originally posted twice)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.