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
https://forums.phpfreaks.com/topic/89554-get-monday-date-of-x-date/
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));

}

?>

<?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); 

?>

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));

?>

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);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.