ruddernz Posted March 24, 2006 Share Posted March 24, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5654-date-help/ Share on other sites More sharing options...
AV1611 Posted March 24, 2006 Share Posted March 24, 2006 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 monthecho every day/date in a loop that counts backward until the day==friday;-P Quote Link to comment https://forums.phpfreaks.com/topic/5654-date-help/#findComment-20241 Share on other sites More sharing options...
kenrbnsn Posted March 25, 2006 Share Posted March 25, 2006 Here is a function I finally came up with ....[code]<?phpfunction 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) Quote Link to comment https://forums.phpfreaks.com/topic/5654-date-help/#findComment-20444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.