Jump to content

Date help


ruddernz

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.