RIRedinPA Posted January 7, 2011 Share Posted January 7, 2011 I'm sure this is pretty simple I just can't find any examples with The Google. If I am being passed a date as a string in this format "01/06/11" how can I figure out what the current day is and then get the Monday of that week? I tried: $monday = getMonday("01/06/11"); function getMonday($passeddate) { $thisdate = date($passeddate); $weekday = date('w', mktime(0,0,0,$thisdate)); //$weekday here is 5, which works with Sunday as the first day of the week $monday = date('m/d/Y', mktime(0,0,0, $thisdate, $thisdate-(weekday-1), $thisdate); //this gave me 09-07-2010, which makes sense to some degree, obviously the code above is subtracting 4 months // so I assume I need to do something like this // $monday = date('m/d/'Y', mktime(0,0,0, month of $thisdate, day of $thisdate - ($weekday - 1), year of $thisdate)); // I am at a loss as to how to code that... } Link to comment https://forums.phpfreaks.com/topic/223712-date-help/ Share on other sites More sharing options...
RIRedinPA Posted January 7, 2011 Author Share Posted January 7, 2011 Figured it out: function getWeekDays($date) { //convert string to date $thisdate = date($date); $dow = idate('w', mktime(0,0,0,$thisdate)); $offset = ($dow -1); if ($offset <0) { $offset = 6; } $monday = date("m/d/Y", mktime(0,0,0,date('m'), date('d')-$offset, date('Y') )); return $monday; } Link to comment https://forums.phpfreaks.com/topic/223712-date-help/#findComment-1156377 Share on other sites More sharing options...
Pikachu2000 Posted January 8, 2011 Share Posted January 8, 2011 Just another way to do it with strtotime() instead of mktime. unction MONDAY($date) { $offset = ''; if( date('l', strtotime($date)) == 'Monday' ) { $offset = ''; } elseif( date('l', strtotime($date)) == 'Sunday' ) { $offset = 'Tomorrow'; } else { $offset = 'Last Monday'; } return date('M j Y', strtotime("$date $offset") ); } echo MONDAY('01/02/2011'); // returns 'Jan 3 2011' echo MONDAY('2011-01-03'); // returns 'Jan 3 2011' echo MONDAY('January 1, 2011'); // returns 'Dec 27 2010' Link to comment https://forums.phpfreaks.com/topic/223712-date-help/#findComment-1156483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.