current month in php

Today, We want to share with you current month in php.In this post we will show you php get current month full name, hear for php get months from date we will give you demo and example for implement.In this post, we will learn about Everything You Need To Know About Date Format In PHP with an example.

php display current Date & time

get today's date in php, php get current date and time, get todays date in php, php get current date time, how to get today's date in php,
php get current date and time

<?php 
$get_month = date('m');
echo "Current Month of a date: = ".$get_month;
if($get_month == 12){
   echo "<br />December is the month :)";
} else {
   echo "<br /> The month is probably not December";
}
?>

get the current month and previous three months

<?php
echo date("M - Y")."\n";
echo date("M - Y",strtotime("-1 Months"))."\n";
echo date("M - Y",strtotime("-2 Months"))."\n";
echo date("M - Y",strtotime("-3 Months"))."\n";
?>

Results:

Jul - 2021                                                          
Jun - 2021                                                          
May - 2021                                                          
Apr - 2021

Last 6 months from the current month

<?php
for ($i = 1; $i <= 6; $i++) 
{
   $months[] = date("Y-m%", strtotime( date( 'Y-m-01' )." -$i months"));
}
var_dump($months);
?>

Results:

array(6) {                                                  
  [0]=>                                                     
  string(8) "2021-01%"                                      
  [1]=>                                                     
  string(8) "2020-12%"                                      
  [2]=>                                                     
  string(8) "2020-11%"                                      
  [3]=>                                                     
  string(8) "2020-10%"                                      
  [4]=>                                                     
  string(8) "2020-09%"                                      
  [5]=>                                                     
  string(8) "2020-08%"                                      
} 

Prints the day

echo date("l") . "<br>";

Prints the day, date, month, year, time, AM or PM

echo date("l jS \of F Y h:i:s A") . "<br>";

Prints October 3, 1975 was on a Friday

echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br>";

Use a constant in the format parameter

echo date(DATE_RFC822) . "<br>";

prints something like: 1975-10-03T00:00:00+00:00

echo date(DATE_ATOM,mktime(0,0,0,10,3,1975));

Convert seconds into days, hours, minutes and seconds

<?php
function convert_seconds($seconds) 
 {
  $dt1 = new DateTime("@0");
  $dt2 = new DateTime("@$seconds");
  return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
  }
echo convert_seconds(200000)."\n";
?>

Sample Output:

2 days, 7 hours, 33 minutes and 20 seconds

Number of the month before the current month

<?php
echo date('m', strtotime('-1 month'))."\n";
?>

Sample Output:

06

Calculate weeks between two dates

<?php
function week_between_two_dates($user_date_first, $user_date_second)
{
    $first = DateTime::createFromFormat('m/d/Y', $user_date_first);
    $second = DateTime::createFromFormat('m/d/Y', $user_date_second);
    if($user_date_first > $user_date_second) return week_between_two_dates($user_date_second, $user_date_first);
    return floor($first->diff($second)->days/7);
}

$dt1 = '1/1/2021';
$dt2 = '12/31/2021';
echo 'Weeks between '.$dt1.' and '. $dt2. ' is '. week_between_two_dates($dt1, $dt2)."\n";
?>

PHP date() Function Example

<!DOCTYPE html>
<html>
<body>

<?php
// Prints the day
echo date("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A") . "<br>";

// Prints October 3, 1975 was on a Friday
echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br>";

// Use a constant in the format parameter
echo date(DATE_RFC822) . "<br>";

// prints something like: 1975-10-03T00:00:00+00:00
echo date(DATE_ATOM,mktime(0,0,0,10,3,1975));
?>

</body>
</html>

Results:

Wednesday
Wednesday 25th of November 2020 10:12:25 AM
Oct 3,1975 was on a Friday
Wed, 25 Nov 20 10:12:25 +0000
1975-10-03T00:00:00+00:00

“get current month php”

//here display current month
//half name in words
date('M');
//full name in words
date('F');
//number
date('m');

I hope you get an idea about php – get current month number.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Working with datetime in PHP Examples

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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