Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to display current month and year like "Aug 2016" in PHP?

What is the shortest, simplest code to generate the curent month in Full English like September or in abbreviated three letter version like Feb and then add the current Year 2011?

So the code will, depending on the month and year, echo things like:

August 2016 or Aug 2016 etcettera. Thanks!

like image 757
Sam Avatar asked Mar 18 '11 01:03

Sam


People also ask

How can I get current month in php?

php $transdate = date('m-d-Y', time()); echo $transdate; $month = date('m', strtotime($transdate)); if ($month == "12") { echo "<br />December is the month :)"; } else { echo "<br /> The month is probably not December"; } ?>

How can get current year start and end date in php?

$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);


2 Answers

Full version:

<? echo date('F Y'); ?> 

Short version:

<? echo date('M Y'); ?> 

Here is a good reference for the different date options.

update

To show the previous month we would have to introduce the mktime() function and make use of the optional timestamp parameter for the date() function. Like this:

echo date('F Y', mktime(0, 0, 0, date('m')-1, 1, date('Y'))); 

This will also work (it's typically used to get the last day of the previous month):

echo date('F Y', mktime(0, 0, 0, date('m'), 0, date('Y'))); 

Hope that helps.

like image 149
RDL Avatar answered Sep 20 '22 05:09

RDL


Here is a simple and more update format of getting the data:

   $now = new \DateTime('now');    $month = $now->format('m');    $year = $now->format('Y'); 
like image 25
shacharsol Avatar answered Sep 19 '22 05:09

shacharsol