How to find the last day of the month from date in php?

Share:
PHP date function


To find the last day of the month from a date in PHP, you can use the date() and strtotime() functions to manipulate the date. Here's an example:


  $date = "2023-03-15"; // the date you want to find the last day of the month for
  $lastDay = date("Y-m-t", strtotime($date));
  echo $lastDay; // output: 2023-03-31
  


In this example, we first set the $date variable to the date we want to find the last day of the month for. Then we use the strtotime() function to convert the date to a Unix timestamp, which allows us to manipulate it.

Next, we use the date() function with the "Y-m-t" format to get the last day of the month for the given date. The "t" format character returns the number of days in the month, which in turn gives us the last day of the month. Finally, we output the result using the echo statement.

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

Ads