iPixel Posted October 19, 2009 Share Posted October 19, 2009 So... i want to get yesterdays date. that's easy. <?php ### Figure Out Yesterdays Date ### $xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']); echo date('M-j-Y',$xtap_yesterday); ?> My issue is, i dont care for weekends, so if today is Monday then yesterday should be friday, and if today is wednesday then yesterday is tuesday. My best guess at this is somehow loop todays date, and subtract 1 from each go-around, and everytime i get a date MM-DD-YYYY test it against mktime or something and see if it's a weekend. But i'm not sure how. This also should take into consideration if let's say to day is October 1st, then yesterday should be September 30th, while still keeping weekends omitted in mind. Anybody have a clue how i could get my hands on this one? Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/ Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 tell me if this works for you: <?php ### Figure Out Yesterdays Date ### $xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']); print date("M-j-Y",$xtap_yesterday)."\n"; $yesterday = date("w",$xtap_yesterday); switch ($yesterday){ case "6": $xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-2,$xtap_date['year']); break; case "7": $xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-3,$xtap_date['year']); break; default: } echo date('M-j-Y',$xtap_yesterday); ?> Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939918 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Not sure if it's the best solution, but this would probably work... $yesterday = date("l", strtotime("yesterday")); if($yesterday == "Saturday" || $yesterday == "Sunday") { echo date("M-j-Y", strtotime("last friday")); } else { echo date("M-j-Y", strtotime("yesterday")); } Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939920 Share on other sites More sharing options...
jonsjava Posted October 19, 2009 Share Posted October 19, 2009 Not sure if it's the best solution, but this would probably work... $yesterday = date("l", strtotime("yesterday")); if($yesterday == "Saturday" || $yesterday == "Sunday") { echo date("M-j-Y", strtotime("last friday")); } else { echo date("M-j-Y", strtotime("yesterday")); } Bah! I was just using his existing code. You had to ruin my horrible code that way *sigh* Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939922 Share on other sites More sharing options...
iPixel Posted October 19, 2009 Author Share Posted October 19, 2009 That prints Oct-18-2009 Oct-18-2009 when it should pring Oct-16-2009 Oct-16-2009 tell me if this works for you: <?php ### Figure Out Yesterdays Date ### $xtap_yesterday = mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-1,$xtap_date['year']); print date("M-j-Y",$xtap_yesterday)."\n"; $yesterday = date("w",$xtap_yesterday); switch ($yesterday){ case "6": $xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-2,$xtap_date['year']); break; case "7": $xtap_yesterday =mktime(0,0,0,$xtap_date['mon'],$xtap_date['mday']-3,$xtap_date['year']); break; default: } echo date('M-j-Y',$xtap_yesterday); ?> Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939924 Share on other sites More sharing options...
salathe Posted October 19, 2009 Share Posted October 19, 2009 Following on from cags's post, strtotime is your friend but it can be done much simpler using the phrase previous weekday. As usual with strtotime, if you want to make it relative to a specific date (defaults to today) then just provide the timestamp as a second argument. Magic $last_weekday = strtotime("previous weekday"); echo "Previous weekday was " . date("D d M"); Just as a quick test, try: foreach (range(1,21) as $dom) { $today = strtotime("$dom Oct 2009"); $wkday = strtotime("previous weekday", $today); echo "Previous weekday for " . date("D d M", $today) . " is " . date("D d M\n", $wkday); } Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939928 Share on other sites More sharing options...
iPixel Posted October 19, 2009 Author Share Posted October 19, 2009 As they say... Like a glove!!! TY it seems to be working. Unfortunately i wont know 100% untill tomorrow haha. But it got oct-16-2009. I never though of using strtotime(). Not sure if it's the best solution, but this would probably work... $yesterday = date("l", strtotime("yesterday")); if($yesterday == "Saturday" || $yesterday == "Sunday") { echo date("M-j-Y", strtotime("last friday")); } else { echo date("M-j-Y", strtotime("yesterday")); } Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939931 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 Didn't realise strtotime supported previous weekday, I shall bare that in mind in future. Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939942 Share on other sites More sharing options...
iPixel Posted October 19, 2009 Author Share Posted October 19, 2009 You Guys Rock! Thanks for making my life easier hehe. Link to comment https://forums.phpfreaks.com/topic/178263-solved-getting-yesterdays-date-with-a-twist/#findComment-939947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.