?
Solved

PHP: retrieve month and year of the next month

Posted on 2014-01-08
15
Medium Priority
?
797 Views
Last Modified: 2014-01-09
Hi,

I need a PHP function that will give me the written month (in Dutch) + Year of the next month (based on current date).

Something like GetMonthYear(Date);  // today is January 8th 2014

Result:
Februari 2014   (dutch for February 2014)

Translation of the month can probably be done in an array.

Found this function on the web to increase the month.
<?php 
function add_date($orgDate,$mth){ 
  $cd = strtotime($orgDate); 
  $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$cd)+$mth,date('d',$cd),date('Y',$cd))); 
  return $retDAY; 
} 
?>

Open in new window

0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39764993
This should do it
<?php
$month = array (
	'Januari',
	'Februari',
	'Maart',
	'April',
	'Mei',
	'Juni',
	'Juli',
	'Augustus',
	'September',
	'Oktober',
	'November',
	'December'
);

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo $month[intval($date->format('m')) - 1] . ' ' . $date->format('Y');
?>

Open in new window

0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39765045
Here is a better solution using setlocale instead of the array
<?php
setlocale(LC_TIME, 'nld_NLD');

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));
?>

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 39765351
Thanks.
Can you show me how I can use this as a function?
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39765463
If you want the month in Dutch (NL) or a couple of other languages, this will do the job.  PHP setLocale() means well but can be tricky to get right.  The man page says, " Different systems have different naming schemes for locales."  There are some illuminating and cautionary user-contributed notes on the man page!

Lines 14-93 translate the months reliably
Lines 97-110 defines the GetMonthYear() function

Your script will need to include both functions.

<?php // RAY_temp_delphiwizard.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28333185.html
// REF http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// REF http://www.omniglot.com/language/time/months.htm

// THIS RETURNS IN ENGLISH - INVALID LOCALE?  NO MESSAGE AT ALL!
setlocale(LC_TIME, 'nld_NLD');
$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));

// MONTHS IN DIFFERENT LANGUAGES
function myMonth($lang='EN', $date='TODAY')
{
    // CONSTRAIN MONTH NUMBER
    $ts = strtotime($date);
    if (!$ts) $ts = time();
    $m = date('m', $ts);

    $lang = strtoupper(substr(trim($lang),0,2));

    // MONTHS IN LANGUAGES
    $months = array
    ( 'NL' => array
      ( '01' => 'januari'
      , '02' => 'februari'
      , '03' => 'maart'
      , '04' => 'april'
      , '05' => 'mei'
      , '06' => 'juni'
      , '07' => 'juli'
      , '08' => 'augustus'
      , '09' => 'september'
      , '10' => 'oktober'
      , '11' => 'november'
      , '12' => 'december'
      )
    , 'FR' => array
      ( '01' => 'janvier'
      , '02' => 'février'
      , '03' => 'mars'
      , '04' => 'avril'
      , '05' => 'mai'
      , '06' => 'juin'
      , '07' => 'juillet'
      , '08' => 'août'
      , '09' => 'septembre'
      , '10' => 'oktobre'
      , '11' => 'novembre'
      , '12' => 'decembre'
      )
    , 'DE' => array
      ( '01' => 'Januar'
      , '02' => 'Februar'
      , '03' => 'März'
      , '04' => 'April'
      , '05' => 'Mai'
      , '06' => 'Juni'
      , '07' => 'Juli'
      , '08' => 'August'
      , '09' => 'September'
      , '10' => 'Oktober'
      , '11' => 'November'
      , '12' => 'Dezember'
      )
    , 'IT' => array
      ( '01' => 'gennaio'
      , '02' => 'febbraio'
      , '03' => 'marzo'
      , '04' => 'aprile'
      , '05' => 'maggio'
      , '06' => 'giugno'
      , '07' => 'luglio'
      , '08' => 'agosto'
      , '09' => 'settembre'
      , '10' => 'ottobre'
      , '11' => 'novembre'
      , '12' => 'dicembre'
      )
    )
    ;

    // IF ENGLISH
    if ($lang == 'EN') return date('F', $ts);

    // IF UNKNOWN LANGUAGE
    if (!array_key_exists($lang, $months)) return date('F', $ts);

    // IF KNOWN LANGUAGE
    return $months[$lang][$m];
}


// FOR THE ANSWER AT EE
function getMonthYear($date='TODAY')
{
    // CONSTRAIN MONTH NUMBER
    $ts = strtotime($date);
    if (!$ts) $ts = time();

    // NEXT MONTH (MAYBE NEXT YEAR)
    $newdt = date("Y-m-01", $ts);
    $newts = strtotime($newdt . ' + 1 MONTH');
    $newmo = date('m', $newts);
    $newyr = date('Y', $newts);

    return myMonth('NL', $newyr . '-' . $newmo . '-01') . " $newyr";
}

// TEST THE FUNCTION
echo '<pre>';
echo PHP_EOL . getMonthYear();
echo PHP_EOL . getMonthYear('December, 2014');
echo PHP_EOL . getMonthYear('TODAY - 78 WEEKS');

Open in new window

HTH, ~Ray
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39765713
As your initial requirement says to use the current date - here is a function that does what you require.
function get_month_year()
{
  setlocale(LC_TIME, 'nld_NLD');

  $date = new DateTime();
  $date->add(new DateInterval('P1M'));
  return strftime("%B %Y", strtotime($date->format('Y-m')));
?>
}

Open in new window

Re the comments on setLocale - it does require some tweaking depending on whether you are running on windows or Linux - but you should know which system you are runnign on and it is a trivial thing to be able to set the correct value. You could even write a bit of code that auto detects and calls the right parameter.
Either way - a lot better than having to maintain an array of months.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39765720
@Ray
// THIS RETURNS IN ENGLISH - INVALID LOCALE?  NO MESSAGE AT ALL!
setlocale(LC_TIME, 'nld_NLD');

That works on Windows systems

For Linux

setLocale(LC_TIME, 'nl_NL')

Should do the trick
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39765784
Yes, that's why I wrote...
The man page says, " Different systems have different naming schemes for locales."  There are some illuminating and cautionary user-contributed notes on the man page!
Also, this is kind of a grin... When was the last time you needed an update to the list of months?  Not in my lifetime, IIRC :-)
better than having to maintain an array of months
0
 

Author Closing Comment

by:Delphiwizard
ID: 39766391
@Ray:This solution has much more code, though I can strip the other languages. Most important it works without me having to make any changes.
@JulianH: Your code gives me just an errormessage and I believe that local settings might influence the outcome, where Ray's code will always produce the same result.
Thank you both.
0
 

Author Comment

by:Delphiwizard
ID: 39766398
@JulianH: the error I get is:
Fatal error: Call to undefined method DateTime::add() in /home/mydomain/domains/mysite.nl/public_html/includes/functions/functions.shopcart.php on line 269
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39767274
If you are using a version of PHP < 5.2 then that would explain the issue.
0
 

Author Comment

by:Delphiwizard
ID: 39767347
@JulianH: I don't know which version is installed. It's maintained by the webhosting company. I'm in the impression that they keep the servers updated with the latest versions, but that might not always be the case. Thank you for the info.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39767371
To find out the version create this file
pi.php
<?php
phpinfo();
?>

Open in new window

Upload and call that script - version will be displayed at the top of the page. If running on less than 5.2 you might want to check if you can upgrade - PHP currently at version 5.4

Here is a link to the code on a windows server. The script has an extra line to determine OS and set locale string correctly

And here is the code running at that site.
<?php
// Set locale string based on OS
$locale = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'nld_NLD' : 'nl_NL';
setlocale(LC_TIME, $locale);

$date = new DateTime();
$date->add(new DateInterval('P1M'));
echo strftime("%B %Y", strtotime($date->format('Y-m')));
?>

Open in new window

Slightly less longwinded than the accepted solution but does require 5.2 or higher
0
 

Author Comment

by:Delphiwizard
ID: 39767541
PHP Version 5.2.17
Running the new script still gives the error.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39767563
0
 

Author Comment

by:Delphiwizard
ID: 39767581
Thank you. Now we know what went wrong...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question