Forum Moderators: coopster

Message Too Old, No Replies

Find the previous Monday given any date

         

zulubanshee

3:03 am on Mar 25, 2009 (gmt 0)

10+ Year Member



Say a user selects a date--call it Wed 2009-02-18.
I need to find out the date of the previous Monday. With respect to the this example, I want to get 2009-02-16.

zulubanshee

3:06 am on Mar 25, 2009 (gmt 0)

10+ Year Member



Incidentally, all dates will be in the past.

ag_47

3:34 am on Mar 25, 2009 (gmt 0)

10+ Year Member



May be a better way, but simple one:


$today = $user_selected-date; //in Unix timestamp format (1234.. )
while(date('D', $today) != 'Mon'){
$today -= 60*60*24; //minus 1 day
}

zulubanshee

3:51 am on Mar 25, 2009 (gmt 0)

10+ Year Member



I found this on this board. A little longer but it works :)

$now = date('Y-m-d h:m:s', strtotime("now"));
echo $now."<br/>";
$some_date = '2009-01-25';
$found = 0;
$i=0;
while(!$found)
{
$find_monday = date("Y-m-d", strtotime('+'.$i.' monday'));
if($some_date > $find_monday )
{
echo "$find_monday is the monday you were looking for<br/>";
$found = 1;
break;
}
else
$i--;
}

eeek

4:54 am on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$today = $user_selected-date; //in Unix timestamp format (1234.. ) 
while(date('D', $today) != 'Mon'){
$today -= 60*60*24; //minus 1 day
}

You want to loop to find a Monday? It'd be a lot easier to get the day of the week as a number and subject the proper number of days to get to Monday.

Habtom

5:12 am on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like the following:

date("Y-m-d", strtotime("Previous Monday", $selectedate));

// $selectedate = timestamp of the date selected

[edited by: Habtom at 5:14 am (utc) on Mar. 25, 2009]

rocknbil

4:00 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



USE MYSQL! :-) This makes it so very easy . . . .

For just previous Monday,

select date_add(curdate(), interval 0-weekday(curdate()) day);

Monday and Friday,

select date_add(curdate(), interval 0-weekday(curdate()) day) as Monday, date_add(curdate(), interval 4-weekday(curdate()) day) as Friday;

.... etc. ....

zulubanshee

8:36 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



ok thank you all