How to find the last saturday of the current month in PHP

In this article, we will discuss how to find the last Saturday of the month using PHP.

Predefined function to find the last Saturday in PHP

PHP features a method called strtotime() method to create a UNIX timestamp from textual data.

The below example illustrates a simple script to find last Saturday,




<?php
//returns the timestamp from strtotime method
echo date("l, d-M-Y", strtotime("last saturday of this month"));
?>

Output:-

Saturday, 27-Jul-2019

Note:-

  • In the above script, “l” displays the day from the strtotime() method.
  • In the above script, “d-M-Y” is the format for time i.e dd-mm-yyyy.

Let us discuss another way to find last Saturday of the month i.e with the mktime() method,

<?php
//function to find last Saturday
function getLastSaturday($day)
{
return date('d/m/y',strtotime($day,mktime(0,0,0,date("n")+1,1))); 
}
//String to find Last Saturday
$day="last Saturday";
// this will output current month’s last Saturday
echo getLastSaturday($day);
?>

Output:-

27/07/19

Note:-

  • In the above script, the mktime() – inbuilt method in PHP, it returns Unix timestamp for a date.

In this way, we can find the last Saturday using PHP. If you have any doubts about this article, leave a comment below.

See also,


Leave a Reply

Your email address will not be published. Required fields are marked *