HTML format:
<!DOCTYPE html>
<html>
<head>
<title>Get the previous Monday date in PHP if today is Monday</title>
</head>
<body>
<h1>Get the previous Monday date in PHP if today is Monday</h1>
<p>In this tutorial, we will learn how to get the previous Monday date in PHP if today is Monday. This can be useful when working with date and time calculations in PHP.</p>
<h2>Step 1: Check if today is Monday</h2>
<p>In order to get the previous Monday date, we first need to check if today is Monday. We can use the <code>date()</code> function in PHP to get the current day of the week.</p>
<pre>
<code>
// get current day of the week
$day = date('l');
// check if today is Monday
if($day == 'Monday'){
// code to get previous Monday date
}
else{
// code to display error message
echo "Today is not Monday!";
}
</code>
</pre>
<p>If today is not Monday, we will display an error message. Otherwise, we will proceed to get the previous Monday date.</p>
<h2>Step 2: Calculate the previous Monday date</h2>
<p>To get the previous Monday date, we will use the <code>strtotime()</code> function in PHP. This function allows us to convert a string to a Unix timestamp, which can then be manipulated to get the desired date.</p>
<pre>
<code>
// calculate the previous Monday date
$previous_monday = strtotime('last Monday');
</code>
</pre>
<p>The <code>last Monday</code> string passed to the <code>strtotime()</code> function will return the Unix timestamp of the previous Monday date.</p>
<h2>Step 3: Display the previous Monday date in desired format</h2>
<p>Now that we have the Unix timestamp of the previous Monday date, we can use the <code>date()</code> function again to format it in any way we want.</p>
<pre>
<code>
// format the previous Monday date
$previous_monday_date = date('d-m-Y', $previous_monday);
// display the date
echo "The previous Monday date is: " . $previous_monday_date;
</code>
</pre>
<p>The above code will display the previous Monday date in the format <code>dd-mm-yyyy</code>. You can change the format according to your preference.</p>
<h2>Final Code</h2>
<pre>
<code>
// get current day of the week
$day = date('l');
// check if today is Monday
if($day == 'Monday'){
// calculate the previous Monday date
$previous_monday = strtotime('last Monday');
// format the previous Monday date
$previous_monday_date = date('d-m-Y', $previous_monday);
// display the date
echo "The previous Monday date is: " . $previous_monday_date;
}
else{
// code to display error message
echo "Today is not Monday!";
}
</code>
</pre>
<p>With this code, we can get the previous Monday date in PHP if today is Monday. You can also modify the code to get the next Monday date or any other day of the week.</p>
<h2>Conclusion</h2>
<p>In this tutorial, we learned how to get the previous Monday date in PHP if today is Monday. We used the <code>date()</code> and <code>strtotime()</code> functions to check the current day and calculate the previous Monday date respectively. We also saw how to format the date according to our preference. You can now use this code in your projects to perform date and time calculations in PHP.</p>