Icebergness Posted March 14, 2012 Share Posted March 14, 2012 Hi, As the title says, I'm trying to get the last working day of the month (by this I mean excluding Saturday and Sunday). My thinking was that I basically need to this: If (day = Sat) { -1 day } elseif (day = Sun) { -2 days } else { keep it as it is } So far, all I have is the below to echo out the day of date("t/m/y"), however, all I'm getting is Thursday, whereas the last day of March is a Saturday <?php $lastdateofthemonth = date("t/m/y"); $lastworkingday = date('l', strtotime($lastdateofthemonth)); echo $lastworkingday; ?> Can anyone help, or point me in the way of where this may have been answered in the past? I've searched all over Google but can't find what I'm looking for. Thanks Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/ Share on other sites More sharing options...
tbare Posted March 14, 2012 Share Posted March 14, 2012 try changing it to $lastdateofthemonth = date("m/t/y"); That gets your code to return Saturday -- then you your If statements to get what you need. Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/#findComment-1327315 Share on other sites More sharing options...
tbare Posted March 14, 2012 Share Posted March 14, 2012 I'd also say be careful for holidays - if one falls on a friday, you'll have to figure for that, too (if you need to)... Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/#findComment-1327317 Share on other sites More sharing options...
tbare Posted March 14, 2012 Share Posted March 14, 2012 in case you're still fighting it - here's what I came up with. $lastdateofthemonth = date("Y-m-t"); $lastworkingday = date('l', strtotime($lastdateofthemonth)); if($lastworkingday == "Saturday") { $newdate = strtotime ('-1 day', strtotime($lastdateofthemonth)); $lastworkingday = date ('Y-m-j', $newdate); } elseif($lastworkingday == "Sunday") { $newdate = strtotime ('-2 day', strtotime($lastdateofthemonth)); $lastworkingday = date ( 'Y-m-j' , $newdate ); } echo date('l', strtotime($lastworkingday))." ".$lastworkingday; That should get you what you're after... Good luck! Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/#findComment-1327344 Share on other sites More sharing options...
Icebergness Posted March 14, 2012 Author Share Posted March 14, 2012 Thank you very much tbare, that final script worked an absolute treat. It's easy to forget that PHP is averse to the British date format Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/#findComment-1327351 Share on other sites More sharing options...
tbare Posted March 14, 2012 Share Posted March 14, 2012 haha... yeah... I'm American, and still don't appreciate the date format we use here... Link to comment https://forums.phpfreaks.com/topic/258908-trying-to-find-the-last-working-day-of-the-month/#findComment-1327352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.