PHP Date Exercises : Number of the month before the current month
PHP date: Exercise-20 with Solution
Write a PHP script to get the number of the month before the current month.
Sample Solution:
PHP Code:
<?php
// Echoes the month of the date obtained by subtracting 1 month from the current date
echo date('m', strtotime('-1 month'))."\n";
?>
Output:
06
N.B.: The result may varry for your system date and time.
Explanation:
In the exercise above,
echo date('m', strtotime('-1 month'))."\n";: Outputs the month of the date obtained by subtracting 1 month from the current date.
- date('m', ...) formats the date to display only the month in two digits.
- strtotime('-1 month') calculates the timestamp representing the date and time one month ago from the current date.
- The result is echoed with a newline character ("\n") appended for formatting.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to calculate weeks between two dates.
Next: Write a PHP script to convert seconds into days, hours, minutes and seconds.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php-exercises/php-date-exercise-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics