Avatar of Stef Merlijn
Stef Merlijn
Flag for Netherlands asked on

PHP: retrieve month and year of the next month

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

PHP

Avatar of undefined
Last Comment
Stef Merlijn

8/22/2022 - Mon
Julian Hansen

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

Julian Hansen

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

Stef Merlijn

ASKER
Thanks.
Can you show me how I can use this as a function?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

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.
Julian Hansen

@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
Ray Paseur

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Stef Merlijn

ASKER
@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.
Stef Merlijn

ASKER
@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
Julian Hansen

If you are using a version of PHP < 5.2 then that would explain the issue.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Stef Merlijn

ASKER
@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.
Julian Hansen

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
Stef Merlijn

ASKER
PHP Version 5.2.17
Running the new script still gives the error.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

Stef Merlijn

ASKER
Thank you. Now we know what went wrong...