Jump to content

Get Monday Date of X Date


onlyican

Recommended Posts

Hey all.

 

I am building a script, and I need to get the date of MONDAY for a week (Anywhere from MOnday to Sunday)

 

This is what i come up with, which works.

 

Just interested to know if there are any other methods..

 

function GetMondayDateForWork($DB, $user, $Date){
$DateDay = date("D", $Date);
$OneDay = 60*60*24;
$MondayDate = false;
switch($DateDay){
	case "Mon":
		$MondayDate = $Date;
	break;
	case "Tue":
		echo "Here!";
		$MondayDate = $Date - $OneDay;
	break;
	case "Wed":
		$MondayDate = $Date - ($OneDay * 2);
	break;
	case "Thu":
		$MondayDate = $Date - ($OneDay * 3);
	break;
	case "Fri":
		$MondayDate = $Date - ($OneDay * 4);
	break;
	case "Sat":
		$MondayDate = $Date - ($OneDay * 5);
	break;
	case "Sun":
		$MondayDate = $Date - ($OneDay * 6);
	break;
}
return $MondayDate;
}

Link to comment
Share on other sites

current_date - ((date('w',$date) - 1)*60*60*24)

 

substitute 'current_date' for a unix timestamp (use mkdate() to create one if necessary) for the date for which you want to find the Monday date.

 

I think you forgot to change one of your variable names. "current_date" shoudl be the same variable as $date within the formula:

 

<?php

function GetMondayDateForWork($date){

    return ($date - ((date('w',$date)-1)*60*60*24));

}

?>

Link to comment
Share on other sites

<?php //getday.php function

$current_date = mktime(); //todays epoch date
$day_wanted = 1; //0,1,2,3,4,5,6 Sun - Sat You could build a form to make this dynamic

function GetWeekday($current_date, $day_wanted) {
$current_day = date('w', $current_date); //get todays day of the week 0-6
$adjustment = $day_wanted - $current_day; //get how many days to difference
$day_adjustment = $adjustment * 86400;  //create a positive or negative number reflecting the seconds between today and the weekday you wanted 
$new_date = $current_date + $day_adjustment; //add the negitive or positive number to the current epoch date
return $chosen_day = date('l dS \of F Y', $new_date);
}
echo GetWeekday($current_date, $day_wanted); 

?>

Link to comment
Share on other sites

We mere mortals pound out half a page of code to do something and then someone on here shows you how to do it with half a line.  ;D 

 

Although your solution was not as eloquent, it made up for it in being more flexible. That was a great idea to make it variable for the DOW that you are trying to find. I have updated my one-liner to take advantage of that and added it to my library:

 

<?php

//##############################################
//
// Function: GetDOWDate($date, $dow)
//
// Description:
//   This funtion takes a date (timestamp) and
//   a DOW (day of week 0-6) and returns the
//   timestamp for the DOW in the input date
//
// Parameters:
//  - date: Timestamp for the source date
//  - dow: The Day of Week to return
//
//##############################################
function GetDOWDate($date, $dow){

    return ($date - ((date('w',$date)-$dow)*60*60*24));

}


$currentDate = mktime(); //Today's date
$DOWtoReturn = 2;        //(0-6, Sun-Sat)


echo date('l, m-d-Y', GetDOWDate($currentDate, $DOWtoReturn));

?>

Link to comment
Share on other sites

I use this, so it works for Sunday too

 

<?php
function Monday($date)
{
    $d = (date('w', $date) + 6) % 7;
    return date ('d M Y', strtotime("-$d days", $date));
}
?>

 

to test it

 

<?php 
$dt = mktime(0,0,0,2,4,2008);
for ($i=0; $i<10; $i++)
{
    echo date ('D d M y', $dt), ' ------- Week start: ', Monday($dt), '<br>';
    $dt = strtotime("+1 days", $dt);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.