[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 806
  • Last Modified:

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

0
Stef Merlijn
Asked:
Stef Merlijn
  • 7
  • 6
  • 2
1 Solution
 
Julian HansenCommented:
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
 
Julian HansenCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
Thanks.
Can you show me how I can use this as a function?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
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
 
Julian HansenCommented:
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
 
Julian HansenCommented:
@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
 
Ray PaseurCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
@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
 
Stef MerlijnDeveloperAuthor Commented:
@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
 
Julian HansenCommented:
If you are using a version of PHP < 5.2 then that would explain the issue.
0
 
Stef MerlijnDeveloperAuthor Commented:
@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
 
Julian HansenCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
PHP Version 5.2.17
Running the new script still gives the error.
0
 
Julian HansenCommented:
0
 
Stef MerlijnDeveloperAuthor Commented:
Thank you. Now we know what went wrong...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now